note 51150 deleted from language.oop5.patterns by danbrown

From: Date: Sun, 23 Jan 2011 16:48:59 +0000
Subject: note 51150 deleted from language.oop5.patterns by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176128@lists.php.net to get a copy of this message
Note Submitter: JimmyPaterson at gmx dot de ---- Since the __CLASS__ constant is used, the Singleton pattern won't work as a Base Class which you would want to implement into your classes. Thus the following will _not_ work: <?php class Singleton { private static $_instance; final private function __construct() { } public static function getSingleton() { if(!isset(self::$_instance)) { $tmpName = __CLASS__; self::$_instance = new $tmpName; } return self::$_instance; } public function NameSingletonClass() { echo __CLASS__; } public function __clone() { } } class MyClass extends Singleton { public function NameMyClass() { echo __CLASS__; } } $instance = MyClass::getSingleton(); $instance->NameSingletonClass(); // you'll get a nice Call to undefined method Songleton::NameMyClass() Error $instance->NameMyClass(); ?> Kind of stupid if one has to write a Singleton-Pattern for each Class... or did I miss something?

« previous php.notes (#176128) next »