Search code examples
phpinheritancestaticfactory-pattern

inherited factory method should return instances of own class not inherited class


I have a class that has a complex static factory method, lets call it ClassA. I have extended ClassA into ClassB and I want ClassB's factory method to do everything that ClassA's factory method does, except return a ClassB.

class ClassA{
   static public function Factory($construct_args, $contents){
      $new = new ClassA($construct_args);
      //Does lots of stuff with $contents
   }
}

class ClassB extends ClassA{
   //specialty methods and properties
}

echo get_class(ClassB::Factory($construct_args, $contents);

This should echo ClassB

Is there a way I can use ClassA's Factory Method without copy-pasting it to ClassB? I am using PHP5, but not 5.3.0.


Solution

  • To do exactly what you want you need Late Static Binding, so you need PHP 5.3. It can not be done as you want in PHP 5.2. People have tried.

    The workarounds I've seen/used for 5.2 are

    1. As you mentioned (and don't want to do) copy your factory method into each class

    2. Require end-users of the factory to supply the name of the class they're instantiating as an argument

    3. Require your factories to implement an interface or have them inherit from an abstract class (or just know the convention) that requires the definition of a static "getClassName" method, which will be used in the factory. This is similar to number one, but some people prefer it as it allows their factory method to remain in the "bottom" class.