Re: Singleton with variable parameters
| From: | Nathan Nobbe | Date: | Wed, 15 Dec 2010 21:31:05 +0000 |
| Subject: | Re: Singleton with variable parameters | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310064@lists.php.net to get a copy of this message | ||
On Wed, Dec 15, 2010 at 2:11 PM, Sebastian Detert <php-maillist@elygor.de>wrote:
> 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();
>
Until traits release I think you'll find this painful as any class that
extends Singleton must now have extra functionality imbued via interfaces
and composition.
You will probly prefer to use a registry, something like
Registry::getInstance('classname', array('parameters'));
Even if you go with an abstract base class you will want an array of
instances internally.
How can I manage to use this with an unkown number of parameters like Foo ->
> getInstance('a', 'b'); ?
>
yes, func_get_args() & call_user_func_array() however, fyi, these are very
slow.
> 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.
>
if performance is a concern why not use setters to establish state on all of
your classes and keep the getInstance method down to 0 params. As long as
your singletons aren't immutable this should work just fine.
-nathan