note 83984 added to function.get-class
| From: | James at osu1 dot php dot net | Date: | Sun, 22 Jun 2008 09:46:08 +0000 |
| Subject: | note 83984 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140651@lists.php.net to get a copy of this message | ||
To get round this problem, another option is a factory to create singletons.
<?php
class Factory {
private static $instances = array();
public static function getInstance($class_name) {
if(!array_key_exists($class_name, self::$instances)) {
self::$instances[$class_name] = new $class_name();
}
return self::$instances[$class_name];
}
}
?>
I guess you could modify this to create singletons from some, and prototypes for the rest.
There's probably some good way to stop people instantiating objects directly. Perhaps by using
the backtrace in the constructor of the class to ensure that it was indeed the Factory that created
the object.
----
Server IP: 193.95.141.37
Probable Submitter: 83.70.255.66
----
Manual Page -- http://www.php.net/manual/en/function.get-class.php
Edit -- https://master.php.net/note/edit/83984
Del: integrated -- https://master.php.net/note/delete/83984/integrated
Del: useless -- https://master.php.net/note/delete/83984/useless
Del: bad code -- https://master.php.net/note/delete/83984/bad+code
Del: spam -- https://master.php.net/note/delete/83984/spam
Del: non-english -- https://master.php.net/note/delete/83984/non-english
Del: in docs -- https://master.php.net/note/delete/83984/in+docs
Del: other reasons-- https://master.php.net/note/delete/83984
Reject -- https://master.php.net/note/reject/83984
Search -- https://master.php.net/manage/user-notes.php