You have what is called circular addiction . It takes B to complete the construction, and B to complete A. Thus, this continues year-round.
In principle, what happens is that self::$instancefor each class it is not filled until new class()it ends. Therefore, in the constructor, you call another getInstance. But every time you click get_instance(), it self::$instanceremains zero, because the previous one newnever finished. And around and around. He will keep going until the end.
:
class foo {
private static $instance;
private function __construct() {
}
private function setBar(bar $bar) {
$this->bar = $bar;
}
public static function get_instance() {
if (empty(self::$instance)) {
self::$instance = new foo();
self::$instance->setBar(bar::get_instance());
}
return self::$instance;
}
}
class bar {
private static $instance;
public function __construct() {
}
private function setFoo(foo $foo) {
$this->foo = $foo;
}
public static function get_instance() {
if (empty(self::$instance)) {
self::$instance = new bar();
self::$instance->setFoo(foo::get_instance());
}
return self::$instance;
}
}
, , , .