Singleton with variable parameters
| From: | Sebastian Detert | Date: | Wed, 15 Dec 2010 21:11:56 +0000 |
| Subject: | Singleton with variable parameters | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-310060@lists.php.net to get a copy of this message | ||
Hi guys,
I am trying to generate an abstract Singleton class for use in arbitrary classes, like
class Foo extends Singleton {}
and generate a new class with Foo -> getInstance();
How can I manage to use this with an unkown number of parameters like Foo -> getInstance('a', 'b'); ?
Something like "self::$instance = call_user_func (array(static , "__construct"), func_get_args());" instead of "self::$instance = new self;" doesn't work and an eval() is too slow.
I hope you can help me.
Thanks,
Sebastian
class Singleton
{
static private $instance = null;
static public function getInstance()
{
if <http://www.phpbar.de/w/if>(null === self::$instance) {self::$instance = new self; }
return <http://www.phpbar.de/w/return>self::$instance;} private function __construct(){} private function __clone(){} }