This PHP question is related to this question, but is a bit different. I have a static factory method called create()
which instantiates a class instance. I want the method to dynamically instantiate an instance of the (sub)class on which it is called. So, the class it instantiates must be determined at runtime. But I want to do this without having to redefine the static factory method in the subclass (and this would be perfectly valid in my example since the subclass has no new data members to initialize). Is this at all possible?
class Foo {
private $name;
public static function create($name) {
//HERE INSTED OF:
return new Foo($name);
//I WANT SOMETHING LIKE:
//return new get_class($this)($name);//doesn't work
//return self($this);//doesn't work either
}
private function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
// the following class has no private data, just extra methods:
class SubFoo extends Foo {
public function getHelloName() {
echo "Hello, ", $this->getName(), ".\n";
}
}
$foo = Foo::create("Joe");
echo $foo->getName(), "\n"; // MUST OUTPUT: Joe
$subFoo = SubFoo::create("Joe");
echo $subFoo->getHelloName(), "\n"; // MUST OUTPUT: Hello, Joe.
You must create object using Late Static Binding
- method get_called_class()
is useful. Second option is use static
keyword.
Example:
class Foo
{
private $name;
public static function create($name)
{
$object = get_called_class();
return new $object($name);
}
private function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
class SubFoo extends Foo
{
public function getHelloName()
{
return "Hello, ". $this->getName();
}
}
$foo = Foo::create("Joe");
echo $foo->getName(), "\n";
$subFoo = SubFoo::create("Joe");
echo $subFoo->getHelloName(), "\n";
And output:
Joe
Hello, Joe
return new static();
there is already reserved keyword static
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With