Another fun aspect of this is that there is also a static
scope that can be used which references the class of the calling class context as opposed to the defined class context. So the code:
class A {
public static function createNew(){
return new self();
}
}
class B extends A {
}
$test = B::createNew(); // This will actually yield an instance of A
but if class A was defined as
class A {
public static function createNew(){
return new static();
}
}
Then $test = B::createNew();
would yield a instance of B as you would expect.
This is also relevant for static properties, when there is inheritance in play self::$property
and static::$property
can mean two totally different things.
If inheritance and static properties/methods are in play it is important to know the difference and in my experience self is almost always wrong in these cases and it can lead to some fun bugs that only manifest if the more then one member of the class hierarchy is in play at a given time.