note 99978 deleted from language.oop5.patterns by danbrown
| From: | danbrown@php.net | Date: | Fri, 17 Sep 2010 15:09:54 +0000 |
| Subject: | note 99978 deleted from language.oop5.patterns by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172692@lists.php.net to get a copy of this message | ||
Note Submitter: hugo dot phpnet at tbdwebdesign dot nl
----
@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
?>