note 85228 added to function.get-class
| From: | Edward at osu1 dot php dot net | Date: | Thu, 21 Aug 2008 10:26:42 +0000 |
| Subject: | note 85228 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143702@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 213.249.64.162
Probable Submitter: 87.211.158.196
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/85228
Del: integrated -- https://master.php.net/note/delete/85228/integrated
Del: useless -- https://master.php.net/note/delete/85228/useless
Del: bad code -- https://master.php.net/note/delete/85228/bad+code
Del: spam -- https://master.php.net/note/delete/85228/spam
Del: non-english -- https://master.php.net/note/delete/85228/non-english
Del: in docs -- https://master.php.net/note/delete/85228/in+docs
Del: other reasons-- https://master.php.net/note/delete/85228
Reject -- https://master.php.net/note/reject/85228
Search -- https://master.php.net/manage/user-notes.php