-Original Message-
From: Nathan Nobbe [mailto:quickshif...@gmail.com]
Sent: Wednesday, December 15, 2010 1:31 PM
To: Sebastian Detert
Cc: php-general@lists.php.net
Subject: Re: [PHP] Singleton with variable parameters
On Wed, Dec 15, 2010 at 2:11 PM, Sebastian Detert php-
maill...@elygor.dewrote:
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
Sebastian,
If you need to have some way to keep track of all the instances of subclasses
and be able to communicate between them, then you'll have need to have the
self::$instances = array() in the base class, as Nathan mentioned. But
implementing instance map in the base abstract is unnecessary, IMO, as it could
get very messy in the long run. I suggest you look into Interfaces. It will
help make your code easier to manage in the future as your app gets more
sophisticated.
Regards,
Tommy
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php