note 51150 added to language.oop5.patterns
| From: | JimmyPaterson at gmx dot de | Date: | Tue, 22 Mar 2005 01:55:14 +0000 |
| Subject: | note 51150 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86763@lists.php.net to get a copy of this message | ||
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?
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51150
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51150&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51150&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51150&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51150&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51150&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51150&report=yes
Search -- http://master.php.net/manage/user-notes.php