note 86133 modified in function.get-class by danbrown
| From: | danbrown@php.net | Date: | Sat, 04 Oct 2008 15:53:59 +0000 |
| Subject: | note 86133 modified in function.get-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145055@lists.php.net to get a copy of this message | ||
The code just worked fine with PHP 5.3.
Thanks to you it could be rebuild to work with earlier versions of PHP 5.
I just changed the 'static' to 'self' and die() to an Exception.
<?php
// This one extends Singleton with proper class name
class someSingleton extends Singleton {
public static function getInstance() {
parent::$__CLASS__ = __CLASS__;
parent::getInstance();
}
}
// This one does the logic
abstract class Singleton {
protected static $__CLASS__ = __CLASS__;
protected static $instance = null;
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() {
$class = self::getClass();
if (self::$instance === null) {
self::$instance = new $class();
self::$instance->init();
}
return self::$instance;
}
/**
* Returns the classname of the child class extending this class
*
* @return string The class name
*/
private static function getClass() {
$implementing_class = self::$__CLASS__;
$original_class = __CLASS__;
if ($implementing_class === $original_class)
throw new Exception("You MUST provide a <code>protected static \$__CLASS__ =
__CLASS__;</code> statement in your Singleton-class!");
return $implementing_class;
}
}
?>
--was--
The code just worked fine with PHP 5.3.
Thanks to you it could be rebuild to work with earlier versions of PHP 5.
I just changed the 'static' to 'self' and die() to an Exception.
<?php
// This one extends Singleton with proper class name
class someSingleton extends Singleton {
public static function getInstance() {
parent::$__CLASS__ = __CLASS__;
parent::getInstance();
}
}
// This one does the logic
abstract class Singleton {
protected static $__CLASS__ = __CLASS__;
protected static $instance = null;
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() {
$class = self::getClass();
if (self::$instance === null) {
self::$instance = new $class();
self::$instance->init();
}
return self::$instance;
}
/**
* Returns the classname of the child class extending this class
*
* @return string The class name
*/
private static function getClass() {
$implementing_class = self::$__CLASS__;
$original_class = __CLASS__;
if ($implementing_class === $original_class)
throw new Exception("You MUST provide a <code>protected static \$__CLASS__ =
__CLASS__;</code> statement in your Singleton-class!");
return $implementing_class;
}
}
http://php.net/manual/en/function.get-class.php