I have the follow (sample) code:
class Foo {
public function __construct() {
$this->bar = new Bar();
$this->baz = new Bazr();
}
}
class Bar extends Foo {
public function __construct() {
parent::__construct();
$baz = $this->baz->alert();
}
}
class Baz extends Foo {
public function __construct() {
parent::__construct();
}
public function alert() {
echo('Hello!');
}
}
new Foo();
Which will generate an Fatal error: Maximum function nesting level of '100' reached, aborting!. What I want is, exactly this, ofcourse with different code and no errors.
What I want is one way to know when my instance is already created and not allow more instances of same object, avoiding circular reference.
Learned about Singletom, but, nothing worked. Have any idea?
Implement dependence Injection is the solution: