Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
see here: stackoverflow.com/questions/126409/… –  Steve Evers Nov 29 '12 at 16:31
    
That was fast! Thanks, I bookmarked that. :) –  Scotty C. Nov 29 '12 at 16:48
    
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. –  Jimmy Hoffa Nov 29 '12 at 16:58
    
For future reference, the video I was watching was this one: youtube.com/watch?v=4F72VULWFvc –  Scotty C. Nov 29 '12 at 17:03
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.