simple singleton class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

jmut wrote:It is true that it might not be a problem as long as you use same datasource but still....How about using transactions? You sure cannot use more than one db connection instance because you will loose the transaction despide the fact you use same datasource to write to.
Again, there are several combinations. You could have several "db" objects that share the same connection (i.e. it is the connection that is "static" not the instance. You could also have several objects that composite the same result or transaction object. My point is that the design decision is "What do I want only one of?" It may be an instance of a class or it may be a resource identifier or simply common access to the same resource or something else.

And in PHP the question if often "Could more than one object realistically exist?" because the scripts are so short and focused that there just is not the condition where, for example, multiple Models can be created. So my questions are "What needs to be unique?" and "Am I designing for something that can really happen?"

A Singleton is one way to achieve one kind of unique.
(#10850)
Post Reply