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
RE: [PHP] Singleton with variable parameters
-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
Re: [PHP] Singleton pattern question
Hi,. Unfortunately in PHP this may be true, however in j2ee if you use the singleton pattern the result would be what you expect. happy new year. Hardik Doshi wrote: Hi Group, Currently i am using the singleton design pattern in one of my projects. I was under impression that by using the singleton pattern i need to initialize system configuration only one time (At the time of login) but once i implemented the system, i came to know that system initializes an instance on every request of the webpage (On every request, singleton class reads the system configuration and returns the new instance). Is that the normal nature of using the singleton pattern in the web environment? Please let me know. Thanks Hardik __ Do you Yahoo!? Find out what made the Top Yahoo! Searches of 2003 http://search.yahoo.com/top2003 -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha.com/megaupload Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Singleton Was: [PHP] OO parent/child relationship
One quick thought: You might want to add the following to your if statement: else { $this-_cache[$key] = new $key; return $this-_cache[$key]; } Andy -Original Message- From: Gerard Samuel [mailto:[EMAIL PROTECTED] Sent: Monday, October 06, 2003 5:36 PM To: 'PHP List' Subject: [PHP] Singleton Was: [PHP] OO parent/child relationship Gerard Samuel wrote: Andy Crain wrote: This all seems like a perfect case for the singleton pattern. See http://www.phppatterns.com/index.php/article/articleview/6/1/1/ and http://www.phppatterns.com/index.php/article/articleview/75/1/1/ Andy Im currently trying to wrap the brain around the Singleton Registry article. Wish me luck :) http://www.trini0.org/index.php This is based on work seen at http://www.phppatterns.com/index.php/article/articleview/75/1/1/ Yes yes, I know. It uses $GLOBALS, (OO Sin). Father forgive me From the article, Im getting lost with the reason/meaning/purpose of the instance() method. If anyone can clarify it for me, and let me know if my example would need something like it. So if you care to critique it, feel free, Ill try not to be hurt ;) Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Singleton
Use sessions (like session_start() ) -- Andrey Hristov Web Developer Icygen Corporation BUILDING SOLUTIONS http://www.icygen.com On Tuesday 23 October 2001 11:55 am, you wrote: I'd like to to make a Singleton object in PHP4. I'm trying to use a static variable in a function to do that. Every time I use the reload in the Browser client, the file doesn't seem to keep the information (I kind of expected that). I could think only 2 ways to do that. 1. Using shared memory - which I don't think is really an option for it would make the code to difficult to manage later on. 2. Using environment variables - which would not be good to store any kind of data, such as a pointer to an open file. Any suggestions anyone ? Thanks, Victor -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Singleton
This would be very good to access a Singleton with one user, but how would it be done for all users to acess the same object. Is it possible to hold and open file pointer in this object ? Thanks, Victor -Original Message- From: Andrey Hristov [mailto:[EMAIL PROTECTED]] Sent: terça-feira, 23 de outubro de 2001 19:03 To: [EMAIL PROTECTED] Subject: Re: [PHP] Singleton Use sessions (like session_start() ) -- Andrey Hristov Web Developer Icygen Corporation BUILDING SOLUTIONS http://www.icygen.com On Tuesday 23 October 2001 11:55 am, you wrote: I'd like to to make a Singleton object in PHP4. I'm trying to use a static variable in a function to do that. Every time I use the reload in the Browser client, the file doesn't seem to keep the information (I kind of expected that). I could think only 2 ways to do that. 1. Using shared memory - which I don't think is really an option for it would make the code to difficult to manage later on. 2. Using environment variables - which would not be good to store any kind of data, such as a pointer to an open file. Any suggestions anyone ? Thanks, Victor -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]