note 51651 deleted from language.oop5.patterns by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:48:54 +0000 |
| Subject: | note 51651 deleted from language.oop5.patterns by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176127@lists.php.net to get a copy of this message | ||
Note Submitter: scottmattocks at php dot net
----
In respone to JimmyPaterson:
Since the singleton method isn't declared final, you can override it in the children classes.
First you have to change the singleton class a little to accept a classname parameter.
class Singleton
{
// Hold an instance of the class
private static $instance;
// A private constructor; prevents direct creation of object
private function __construct()
{
echo 'I am constructed';
}
// The singleton method
public static function singleton($classname)
{
if (!isset(self::$instance)) {
self::$instance = new $classname;
}
return self::$instance;
}
}
class SubClass extends Singleton {
public static function singleton()
{
parent::singleton(__CLASS__);
}
}
$singletonInstance = SubClass::singleton();