Re: green bean question on singleton php5

From: Date: Wed, 16 Jan 2008 22:11:22 +0000
Subject: Re: green bean question on singleton php5
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-267456@lists.php.net to get a copy of this message
Eric Butera schreef:
On Jan 16, 2008 4:55 PM, Jochem Maas <jochem@iamjochem.com> wrote:
Eric Butera schreef:
On Jan 16, 2008 4:13 PM, Nathan Nobbe <quickshiftin@gmail.com> wrote:
On Jan 16, 2008 3:59 PM, Jochem Maas <jochem@iamjochem.com> wrote:
given that dbaccess doesn't extend mysqli instantiation of dbaccess is completely pointless no?
i dont know; i think using an instance of dbaccess to control a single instance of the mysqli class is appropriate. personally, i wouldnt extend mysqli unless i had a good reason to. i would probly only rationalize this if i wanted to override some of the behavior in a specific way, or extend it in some meaningful way (even then i might use composition). if that isnt the intention i think composition is the best choice for the singleton. more a matter of preference than anything i suppose. at least i cant think of a better reason to argue for composition.
no idea what you mean
ya; some of these posts from you are a little hard to understand, julian. -nathan
I still don't understand the obsession of a singleton in regards to a db connection. Using a registry is a much better practice I think.
I think I alluded to the registry pattern in my reply above - although I'm not sure. could you care to ellaborate what you mean by registry?
Sure. A registry is just a place to store things. So create an instance of your DB and stick it in the registry and you're done. Then make your script rely on the registry so you can swap stuff out that way instead of having a hard coded reference to some static method that cannot be replaced at runtime. Also with the registry you can use lazy loading. My scripts don't create an instance of the db class until it is actually used. It has the ability to create one at any time by calling the fancy_namespace_Registry::get('db') which will read some config I enabled upstream with the fancy_namespace_Registry::lazy('db', array(config to pass)). In testing I can just replace the db entry with a mock and life is good. Also, my database doesn't connect to the server until an actual method is called that uses it. So just layers of just in time. Of course you could argue that using static methods in the registry is bad too, but I gotta draw the line somewhere! :) There isn't really a right or wrong. Just different ways of doing things. I used to use the singleton with my DB but it became troublesome to test plus always making sure the dependences were pre-loaded was annoying. Now everything takes care of itself.
that makes it clear. thank you. I guess what I was alluding to was a DB [connection object] specific registry. I guess I have never actually thought about it enough because reading your explaination makes it blatantly obvious that the 'pattern' can be made so much more generic ... and I love the generic lazy loading mechanism - I'll be using that idea sharpish :)

« previous php.general (#267456) next »