note 99978 added to language.oop5.patterns
| From: | hugo dot phpnet at tbdwebdesign dot nl | Date: | Fri, 17 Sep 2010 15:07:34 +0000 |
| Subject: | note 99978 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172691@lists.php.net to get a copy of this message | ||
@Deman: It is indeed possible to extend this class Singleton WITHOUT explicitly overriding the
singleton method by using the get_called_class() function:
<?php
class Singleton {
private static $instance;
public static function singleton() {
$classname = get_called_class();//returns the classname from which the inherited function
was called
if (!isset(self::$instance))
self::$instance = array();
if (!isset(self::$instance[$classname]))
self::$instance[$classname] = new $classname;
return self::$instance[$classname];
}
}
class SubClass extends Singleton {
}
class SubSubClass extends SubClass {
}
print_r(SubSubClass::getInstance());//Will show an object of type SubSubClass
?>
----
Server IP: 69.147.83.197
Probable Submitter: 84.27.126.108
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/99978
Del: integrated -- https://master.php.net/note/delete/99978/integrated
Del: useless -- https://master.php.net/note/delete/99978/useless
Del: bad code -- https://master.php.net/note/delete/99978/bad+code
Del: spam -- https://master.php.net/note/delete/99978/spam
Del: non-english -- https://master.php.net/note/delete/99978/non-english
Del: in docs -- https://master.php.net/note/delete/99978/in+docs
Del: other reasons-- https://master.php.net/note/delete/99978
Reject -- https://master.php.net/note/reject/99978
Search -- https://master.php.net/manage/user-notes.php