7

Being a PHP programmer for the last couple of years, I'm just starting to get into advanced programming styles and using polymorphic patterns. I was watching a video on polymorphism the other day, and the guy giving the lecture said that if at all possible, you should get rid of if statements in your code, and that a switch is almost always a sign that polymorphism is needed. At this point I was quite inspired and immediately went off to try out these new concepts, so I decided to make a small caching module using a factory method. Of course the very first thing I have to do is create a switch to decide what file encoding to choose. DANG!

class Main {
 public static function methodA($parameter='')
 {
 switch ($parameter)
 {
 case 'a':
 $object = new \name\space\object1();
 break;
 case 'b':
 $object = new \name\space\object2();
 break;
 case 'c':
 $object = new \name\space\object3();
 break;
 default:
 $object = new \name\space\object1();
 }
 return (sekretInterface $object);
 }
}

At this point I'm not really sure what to do. As far as I can tell, I either have to use a different pattern and have separate methods for each object instance, or accept that a switch is necessary to "switch" between them. What do you guys think?

FrustratedWithFormsDesigner
46.3k7 gold badges130 silver badges176 bronze badges
asked Nov 29, 2012 at 16:25
3
  • see here: stackoverflow.com/questions/126409/… Commented Nov 29, 2012 at 16:31
  • Whoever you were listening to is spot on. Tons of ifs and switches in a code base are an immediate sign the authors don't think in OO. Commented Nov 29, 2012 at 16:58
  • For future reference, the video I was watching was this one: youtube.com/watch?v=4F72VULWFvc Commented Nov 29, 2012 at 17:03

1 Answer 1

1

switch and if are generally required for primitives (which $parameter is), although PHP allows you to create class names and call methods from variables, so you can get away with a little magic.

For example you could do this:

public static function methodA($parameter = '') {
 $class = '\name\space\object' . $parameter;
 return new $class;
}

This of course requires that $parameter and the existing class names are all named appropriately, and it does not account for the default case.

The other alternative requires that $parameter be an object. You can remove the switch, but it doesn't cut down on verbosity (although it is polymorphic). One possible way:

public static function methodA(EncodingType $parameter = null) {
 return new $parameter->getObject();
}

...which would return the required object string.

I should note that the principle of avoiding conditionals in contemplation of using polymorphism is great, especially in theory -- but if you try to eliminate all conditionals in your code you can half kill yourself and end up writing unnecessarily complicated code. It may also be a signal to rethink your design.

I will say that one common spot where you find conditionals is in factory methods, which is what your example seems to be.

answered Nov 29, 2012 at 23:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.