I have a main bootstrap class (singleton1 in the example bellow) that instantiates some singleton classes. In those singleton classes, I need to keep a reference to the app main class for easy and quick reference to it, but doing so gives me a:
Fatal error: Maximum function nesting level of '100' reached, aborting
Here is the exmaple code:
<?php
class Singleton1 {
private static $instance;
private function __construct() {
Singleton2::instance();
}
public static function instance () {
if( !self::$instance ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
public function test () {
echo 'Good.';
}
private function __clone() { }
private function __wakeup() { }
}
class Singleton2 {
private static $instance;
private $singleton1;
private function __construct() {
$this->singleton1 = Singleton1::instance();
}
public static function instance () {
if( !self::$instance ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
public function test () {
$this->singleton1->test();
}
private function __clone() { }
private function __wakeup() { }
}
Singleton1::instance();
Singleton2::instance()->test();
I can't find any logical explanation to the issue ...
Thanks.