When you write new self() inside a member function of a class, you get an instance of that class. This is the magic of the self keyword .
So:
class Foo { public static function baz() { return new self(); } } $x = Foo::baz();
You get Foo even if the static determinant you used was for the derived class:
class Bar extends Foo { } $z = Bar::baz();
If you want to enable polymorphism (in a sense), and PHP pays attention to the classifier you are using, you can change the self keyword for the static :
class Foo { public static function baz() { return new static(); } } class Bar extends Foo { } $wow = Bar::baz();
This is made possible by the PHP function, known as late static binding ; do not confuse it for other, more common uses of the static .
Lightness Races in Orbit Apr 09 '13 at 10:05 2013-04-09 10:05
source share