Hello
I'm using the PHP singleton from Wikipedia. The problem is the singleton has a method that loads and runs other PHP scripts via require once
and when these script are run and implement the Class method get_shared_instance
a new singleton is initiated. Why is this? And what is the work around?
the singleton (in a basic form):
class Controller {
protected static $shared_instance;
public static function get_shared_instance()
{
if (self::$shared_instance === NULL) {
self::$shared_instance = new self();
}
return self::$shared_instance;
}
/// Constructor made private to be used with singleton.
final protected function __construct(){ $this->initiate(); }
/// Do not allow the clone operation: $x = clone $v;
final protected function __clone() { }
the singleton's method that loads the other files
private function settings_by_domain()
{
$current_domain = $_SERVER['HTTP_HOST'];
$path = pathinfo(__FILE__, $options = PATHINFO_DIRNAME);
$settings_file = $path.'/'.$current_domain.'.php';
if (is_file($settings_file))
{
$this->settings_file = $settings_file;
return TRUE;
}
else
{
return FALSE;
}
}
The required file contain:
$c = self::get_shared_instance();
which, unfortunatly, when run creates a new instance?
Many thanks Ross