So I have a parent class that creates instances of a variety of different child classes. Here’s a skeleton:
[code=php]abstract class ParentClass {
public static function create() {
return new <child class>() // instance of calling child class
}
}
class AChildClass extends ParentClass {
}
class AnotherChildClass extends ParentClass {
}
// sample calling code
AChildClass::create(); // should return an instance of AChildClass
AnotherChildClass::create() // should return an instance of AnotherChildClass
So what I need to do is create an instance of the class that called the create() function, but from what I can see, there is no easy way to do this. I have remedied it by setting a static $_class variable in each of the child classes containing the name of the class, then using
[code]new self::$_class()
in the create() method. This seems redundant though, and I figured there must be an easier way…
Anyone got anything?