I don't know if this is Singleton but...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: I don't know if this is Singleton but...

Post by kaisellgren »

josh wrote:
kaisellgren wrote:Having a singleton effectively makes sure no one will ever establish multinuous connection to the database.
That is true, but why would you want to write a database library that can't support multiple connections. For example one adapater for writes and another for reads, in a master-slave replication setup
If we are not talking about master slave setup, then to avoid a coder to write bad code. Establishing a connection to MySQL is very very pricy and should be avoided. I also lazy load the connection, that is the connection is made in the very last minute prior to queries - IF nothing found at cache (both memory and file system caches).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: I don't know if this is Singleton but...

Post by Christopher »

kaisellgren wrote:If we are not talking about master slave setup, then to avoid a coder to write bad code. Establishing a connection to MySQL is very very pricy and should be avoided. I also lazy load the connection, that is the connection is made in the very last minute prior to queries - IF nothing found at cache (both memory and file system caches).
Again, lazy loading, reducing connection overhead, etc. are really unrelated to Singleton. And as also noted, your solution is really to use a global, not a Singleton to solve your problem.

Almost always when you use an object you only want one of that object. Singleton solves the problem where you are getting different instances from different sources unpredictably/uncontrollably. That is a specific and rare problem. And there are other solutions to that problem that can be tried before resorting to Singleton.
(#10850)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: I don't know if this is Singleton but...

Post by allspiritseve »

kaisellgren wrote:You are not using a singleton design pattern for the database class?
No, I'm not.
kaisellgren wrote:Database is often the slowest node of the chain. Having a singleton effectively makes sure no one will ever establish multinuous connection to the database.
I don't understand why that's something to worry about... If someone's establishing another connection to the database, they're doing so explicitly. A singleton isn't going to protect against that (nor should it, as Josh mentioned it may be necessary to have two different connections.)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: I don't know if this is Singleton but...

Post by josh »

Yeah besides what if you want to mock out 10 different adapters for testing, or have an adapter that's hitting memcache, etc.. etc... I could think of counter examples all day that are un-related to master slave. The only performance would be the static call which is probably actually faster since its static, youd have to benchmark it. Personally I'd be worried about the optimization of the SQL going over the connection first.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: I don't know if this is Singleton but...

Post by kaisellgren »

arborint wrote:Again, lazy loading, reducing connection overhead, etc. are really unrelated to Singleton. And as also noted, your solution is really to use a global, not a Singleton to solve your problem.
Yes, I did not say lazy loading and such would be related to Singletons.
arborint wrote:Almost always when you use an object you only want one of that object.
Try telling that to the big world of PHP coders :)

I said it, because the server execution time of a popular shop site I had a look at dropped by roughly 28% after using Singletons. The reason was bad coding within the application and singletons fixed that bad practise. It was not the only way to "fix" the problem, but Singletons were handy.
josh wrote:Personally I'd be worried about the optimization of the SQL going over the connection first.
That one depends so much on the situations. Simple, efficient and small number of queries will not be as bad as multiple connections to the database. Especially if you work with SSL/TSL and remote servers and hundreds of concurrent connection establishments.

One month ago I saw my friend writing a script, which is an Ebay like script and it had this code (something like this):

Code: Select all

$db = new db;
$db2 = new db;
$db3 = new db;
$db4 = new db;
And btw, the constructor initialized the connection to the database. The constructor ate around 95% of the execution time.
allspiritseve wrote:If someone's establishing another connection to the database, they're doing so explicitly.
People explicitly write inefficient and insecure code, I have experienced that way too many times :)
= odino =
Forum Newbie
Posts: 6
Joined: Thu Feb 26, 2009 5:34 am

Re: I don't know if this is Singleton but...

Post by = odino = »

Well, I started an interesting discussion :P
Post Reply