note 83984 deleted from function.get-class by cmb
| From: | cmb@php.net | Date: | Wed, 30 Nov 2016 16:35:12 +0000 |
| Subject: | note 83984 deleted from function.get-class by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207761@lists.php.net to get a copy of this message | ||
Note Submitter: James
----
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.