Avoiding if statements with object oriented design, PHP

Basically, I create a display module for the created advertising system.

I am trying to avoid the following construct with repeating if statements.

The gut feeling tells me a smarter way to do this, perhaps with polymorphism?

<?php class Ad { public $adState = 'active'; } class AdWriter { public function displayAd(Ad $ad, $viewmode = 'visitor') { if ($viewmode =='visitor') { if ($adState == 'active') {} else if ($adState == 'paused') {} else if ($adState == 'inactive') {} } else if ($viewmode = 'owner') { if ($adState == 'active') {} else if ($adState == 'paused') {} else if ($adState == 'inactive') {} } else if ($viewmode == 'administrator') { if ($adState == 'active') {} else if ($adState == 'paused') {} else if ($adState == 'inactive') {} } } } ?> 
+7
source share
6 answers
+9
source

You can create a Factory (template) using the viewmode switch and create a specific Ad that implements an interface with a simple "display" function, for example.

Pseudo example:

 class AdFactory { public static function getAd($sType) { switch($sType) { case "AdOne": return new AdOne(); case "AdTwo": return new AdTwo(); } } throw new Exception("Unknown ad!"); } class AdOne implement AdInterface { public function display() { // All that AdOne does when displaying. } } interface AdInterface { public function display() { } } $oAd1 = AdFactory::getAd('typeOne'); $oAd1->display(); $oAd2 = AdFactory::getAd('typeTwo'); $oAd2->display(); 
+4
source

Instead of passing $ viewmode, pass an object that will encapsulate the logic for this viewmore and call its method, which will do the job. This way you avoid the need for if statements.

+3
source

I sneak into StackOverflow at work, so I don’t have time to write a detailed answer about all your possibilities.

But to "remove" these ifs, you can do this:

 switch $viewmode { case 'visitor': your_code_here; break; case 'owner': your_code_here; break; default: will_run_if_nothing_above_matches; break; } 
0
source
  switch($viewmode){ case "visitor": switch($adstate){ case "active": //statement break; case "paused": break; case "inactive": break; } break; case "owner": break; case "administrator": break; } 
0
source

In chapter 8 of this book you can find a very detailed answer to your question.

In short: use Composition or factories. (see Wesley van Oppdorp answer).

Also, avoid using string arguments as enumerated: $viewmode = 'visitor'
with this argument you will have to store in memory all the possible values ​​of this argument. Or take a look at the function code to remember them. And these values ​​are strings - a good place for typos. In addition, it will be very difficult to change the values ​​in the function, because all calls to this method will contain string strings.
Use class constants:

 class AdWriter { const view_mode_visitor = 1; ... 

Also, $adState - the wrong code must be $ ad-> state. But using public fields is also bad. :)

0
source

All Articles