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 namespaceobject1();
break;
case 'b':
$object = new namespaceobject2();
break;
case 'c':
$object = new namespaceobject3();
break;
default:
$object = new namespaceobject1();
}
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?
3
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 = 'namespaceobject' . $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.