Recently faced with such a problem a simple example:
<?php class a { protected static $self = false; protected function __construct() { } } class b extends a { protected static $self = false; public static function build(){ if(!self::$self){ self::$self = new self(); } return self::$self; } public function doSomeThing() { echo 2; } } class c1 extends a { protected static $self = false; public static function build(){ if(!self::$self){ self::$self = new self(); } return self::$self; } public function doSomeThingElse() { echo 3; } } b::build()->doSomeThing(); c1::build()->doSomeThingElse(); there is a duplicate code in the heirs
protected static $self = false; public static function build(){ if(!self::$self){ self::$self = new self(); } return self::$self; } How can it be taken to the parent class a without disturbing the logic? For example, if we do not specify protected static $self = false; in the successor, we will receive other logic of behavior of the static method build()
staticas a "synonym" of$thisorself. In short, "later static binding" - ArchDemon pm