note 71579 added to language.oop5.patterns
| From: | eyvindh79atgmaildotcom at osu1 dot php dot net | Date: | Wed, 06 Dec 2006 17:48:47 +0000 |
| Subject: | note 71579 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120344@lists.php.net to get a copy of this message | ||
A pure extendable(multiple sublevels) Singleton class in PHP seems impossible at the moment because
of the way PHP handles static inheritance, but my class is close to what i want.
Very simple usage:
<?php
class Test extends Singleton {
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
?>
Singleton class implementation:
<?php
class Singleton {
/***********************
* HOW TO USE
*
* Inherit(extend) from Singleton and add getter:
*
* //public getter for singleton instance
* public static function getInstance(){
* return Singleton::getSingleton(get_class());
* }
*
*/
private static $instanceMap = array();
//protected getter for singleton instances
protected static function getSingleton($className){
if(!isset(self::$instanceMap[$className])){
$object = new $className;
//Make sure this object inherit from Singleton
if($object instanceof Singleton){
self::$instanceMap[$className] = $object;
}
else{
throw SingletonException("Class '$className' do not inherit from
Singleton!");
}
}
return self::$instanceMap[$className];
}
//protected constructor to prevent outside instantiation
protected function __construct(){
}
//denie cloning of singleton objects
public final function __clone(){
trigger_error('It is impossible to clone singleton', E_USER_ERROR);
}
}
?>
Just a simple test case:
<?php
class A extends Singleton {
protected $rndId;
protected function __construct(){
$this->rndId = rand();
}
public function whatAmI(){
echo 'I am a A('.$this->rndId.')<br />';
}
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
class B extends A {
public function whatAmI(){
echo 'I am a B('.$this->rndId.')<br />';
}
public static function getInstance(){
return Singleton::getSingleton(get_class());
}
}
$a = A::getInstance();
$b = B::getInstance();
$a->whatAmI();// should echo 'I am a A(some number)
$b->whatAmI();// should echo 'I am a B(some number)
$a = A::getInstance();
$b = B::getInstance();
$a->whatAmI();// should echo 'I am a A(same number as above)
$b->whatAmI();// should echo 'I am a B(same number as above)
$a = new A();// this should fail
$b = new B();// this should fail
?>
I hope this helps.
-Eyvind-
----
Server IP: 62.73.196.231
Probable Submitter: 80.203.205.114
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/71579
Del: integrated -- https://master.php.net/note/delete/71579/integrated
Del: useless -- https://master.php.net/note/delete/71579/useless
Del: bad code -- https://master.php.net/note/delete/71579/bad+code
Del: spam -- https://master.php.net/note/delete/71579/spam
Del: non-english -- https://master.php.net/note/delete/71579/non-english
Del: in docs -- https://master.php.net/note/delete/71579/in+docs
Del: other reasons-- https://master.php.net/note/delete/71579
Reject -- https://master.php.net/note/reject/71579
Search -- https://master.php.net/manage/user-notes.php