The code in my previous comment was not completely correct. I think this one is.
<?
abstract class Singleton {
protected static $__CLASS__ = __CLASS__;
protected function __construct() {
}
abstract protected function init();
/**
* Gets an instance of this singleton. If no instance exists, a new instance is created and returned.
* If one does exist, then the existing instance is returned.
*/
public static function getInstance() {
static $instance;
$class = self::getClass();
if ($instance === null) {
$instance = new $class();
$instance->init();
}
return $instance;
}
/**
* Returns the classname of the child class extending this class
*
* @return string The class name
*/
private static function getClass() {
$implementing_class = static::$__CLASS__;
$original_class = __CLASS__;
if ($implementing_class === $original_class) {
die("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!");
}
return $implementing_class;
}
}
?>