note 51651 added to language.oop5.patterns
| From: | scottmattocks@php.net | Date: | Wed, 06 Apr 2005 16:40:21 +0000 |
| Subject: | note 51651 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87591@lists.php.net to get a copy of this message | ||
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();
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51651
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51651&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51651&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51651&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51651&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51651&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51651&report=yes
Search -- http://master.php.net/manage/user-notes.php