note 81304 added to function.get-class
| From: | andregs at NOSPAM dot gmail dot NOSPAM dot com | Date: | Thu, 21 Feb 2008 21:17:27 +0000 |
| Subject: | note 81304 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136377@lists.php.net to get a copy of this message | ||
After reading the previous comments, this is the best I've done to get the final class name of
a subclass:
<?php
class Singleton
{
private static $_instances = array();
protected final function __construct(){}
/**
* @param string $classname
* @return Singleton
*/
protected static function getInstance()
{
$classname = func_get_arg(0);
if (! isset(self::$_instances[$classname]))
{
self::$_instances[$classname] = new $classname();
}
return self::$_instances[$classname];
}
}
class Child extends Singleton
{
/**
* @return Child
*/
public static function getInstance()
{
return parent::getInstance(get_class());
}
}
?>
Subclasses must override "getInstance" and cannot override "__construct".
----
Server IP: 69.147.83.197
Probable Submitter: 200.252.229.162
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/81304
Del: integrated -- https://master.php.net/note/delete/81304/integrated
Del: useless -- https://master.php.net/note/delete/81304/useless
Del: bad code -- https://master.php.net/note/delete/81304/bad+code
Del: spam -- https://master.php.net/note/delete/81304/spam
Del: non-english -- https://master.php.net/note/delete/81304/non-english
Del: in docs -- https://master.php.net/note/delete/81304/in+docs
Del: other reasons-- https://master.php.net/note/delete/81304
Reject -- https://master.php.net/note/reject/81304
Search -- https://master.php.net/manage/user-notes.php