<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Snippet: Singletons with PHP</title>
	<atom:link href="http://stut.net/2008/10/20/snippet-singletons-with-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://stut.net/2008/10/20/snippet-singletons-with-php/</link>
	<description>Ramblings of a random software engineer</description>
	<lastBuildDate>Fri, 12 Mar 2010 17:55:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Syym</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-7591</link>
		<dc:creator>Syym</dc:creator>
		<pubDate>Mon, 01 Dec 2008 20:30:13 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-7591</guid>
		<description>I am also using singleton technique. But if you look your code with:

error_reporting(E_ALL &#124; E_STRICT);

You will get many many error messages in your code, where you are using this technique.

Also, if you turn on zend comp. model, you will fail in use singleton.</description>
		<content:encoded><![CDATA[<p>I am also using singleton technique. But if you look your code with:</p>
<p>error_reporting(E_ALL | E_STRICT);</p>
<p>You will get many many error messages in your code, where you are using this technique.</p>
<p>Also, if you turn on zend comp. model, you will fail in use singleton.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stut</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-6596</link>
		<dc:creator>Stut</dc:creator>
		<pubDate>Tue, 21 Oct 2008 14:06:51 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-6596</guid>
		<description>Tom, I understand your point but I still believe the singleton pattern is a tool worth having in your toolbox. My intention with this post was simply to demonstrate how to implement the singleton pattern in PHP, not to suggest that they should be used where they don&#039;t make sense.

The only problem you appear to have with the singleton pattern is the requirement for a private constructor. Make it public and your objections go away. For me the pattern is not so much about control than it is about convenience. Everything related to my database connections is taken care of by one class in one file. If I need more flexibility I simply change that one file to bend the way I need it to while maintaining the existing contract so I don&#039;t need to change any other files.

For most web-based applications a single database connection is the only one that&#039;s needed. In that situation I see no problem using a singleton to manage that connection. If your application needs more than that then a singleton is obviously not the way to go. Likewise with logging as an example, most apps will only need one log so a singleton makes sense there too.

As I mentioned in my previous comment the project I currently spend most of my time on uses a database class that can manage a number of connections but it&#039;s still controlled. The application begins by making one or more calls to DB::AddConnection() passing all the details required for each connection along with a short name by which it can be referred. Anything that then needs a connection calls DB::Connection() and passes the short name of the connection it wants (read, write, stats, etc) and the class ensures that only one resource for each connection is ever created while providing many useful methods for retrieving and manipulating data.

In this particular situation a strict singleton pattern was not suitable so I modified it to manage more than one instance. The constructor is still private and all instance management is handled within the class. This meets all my requirements, none of which have anything to do with the trust I have in the developers on my team.

If you need to be able to create and destroy the database connection for testing purposes all you need to do is add a static method to the class to destroy the singleton instance. Rocket science it ain&#039;t, and for me that requirement is not a good enough reason to avoid the pattern completely.

If you don&#039;t like the idea of a singleton but still want the advantages the functionality can be duplicated using a modified factory pattern where it knows which objects should be singletons. Personally I prefer to keep information like that inside the class in question. This is effectively what your get_db() function is doing, albeit for a single class.</description>
		<content:encoded><![CDATA[<p>Tom, I understand your point but I still believe the singleton pattern is a tool worth having in your toolbox. My intention with this post was simply to demonstrate how to implement the singleton pattern in PHP, not to suggest that they should be used where they don&#8217;t make sense.</p>
<p>The only problem you appear to have with the singleton pattern is the requirement for a private constructor. Make it public and your objections go away. For me the pattern is not so much about control than it is about convenience. Everything related to my database connections is taken care of by one class in one file. If I need more flexibility I simply change that one file to bend the way I need it to while maintaining the existing contract so I don&#8217;t need to change any other files.</p>
<p>For most web-based applications a single database connection is the only one that&#8217;s needed. In that situation I see no problem using a singleton to manage that connection. If your application needs more than that then a singleton is obviously not the way to go. Likewise with logging as an example, most apps will only need one log so a singleton makes sense there too.</p>
<p>As I mentioned in my previous comment the project I currently spend most of my time on uses a database class that can manage a number of connections but it&#8217;s still controlled. The application begins by making one or more calls to DB::AddConnection() passing all the details required for each connection along with a short name by which it can be referred. Anything that then needs a connection calls DB::Connection() and passes the short name of the connection it wants (read, write, stats, etc) and the class ensures that only one resource for each connection is ever created while providing many useful methods for retrieving and manipulating data.</p>
<p>In this particular situation a strict singleton pattern was not suitable so I modified it to manage more than one instance. The constructor is still private and all instance management is handled within the class. This meets all my requirements, none of which have anything to do with the trust I have in the developers on my team.</p>
<p>If you need to be able to create and destroy the database connection for testing purposes all you need to do is add a static method to the class to destroy the singleton instance. Rocket science it ain&#8217;t, and for me that requirement is not a good enough reason to avoid the pattern completely.</p>
<p>If you don&#8217;t like the idea of a singleton but still want the advantages the functionality can be duplicated using a modified factory pattern where it knows which objects should be singletons. Personally I prefer to keep information like that inside the class in question. This is effectively what your get_db() function is doing, albeit for a single class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Barta</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-6595</link>
		<dc:creator>Tom Barta</dc:creator>
		<pubDate>Tue, 21 Oct 2008 12:39:58 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-6595</guid>
		<description>Sorry, that last URL should be http://c2.com/cgi/wiki?SingletonsAreEvil</description>
		<content:encoded><![CDATA[<p>Sorry, that last URL should be <a href="http://c2.com/cgi/wiki?SingletonsAreEvil" rel="nofollow">http://c2.com/cgi/wiki?SingletonsAreEvil</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Barta</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-6594</link>
		<dc:creator>Tom Barta</dc:creator>
		<pubDate>Tue, 21 Oct 2008 12:39:23 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-6594</guid>
		<description>With a singleton, downstream users are programmatically prevented from ever creating more than one instance.  However, with the Database example, there&#039;s no real reason to have such a limitation - it&#039;s not physically impossible for many database connections to exist simultaneously.  If Database is written as a singleton, then the moment I have to make two connections (e.g. interfacing with any legacy systems, or maybe even a one-time-use script that reads from one, does some conversion, then writes to another), I have to modify the class to support multiple concurrent connections.  If Database is a normal class, I just have to create two objects - get_db() only exists to make the usual case easy, and there&#039;s no extra code in there that makes the unusual case hard (including unit-tests that may involve Database construction/destruction).

Check out http://c2.com/cgi/wiki?SingletonsAreEvi  for a discussion on the topic (that wiki is a black hole for free time, seriously).</description>
		<content:encoded><![CDATA[<p>With a singleton, downstream users are programmatically prevented from ever creating more than one instance.  However, with the Database example, there&#8217;s no real reason to have such a limitation &#8211; it&#8217;s not physically impossible for many database connections to exist simultaneously.  If Database is written as a singleton, then the moment I have to make two connections (e.g. interfacing with any legacy systems, or maybe even a one-time-use script that reads from one, does some conversion, then writes to another), I have to modify the class to support multiple concurrent connections.  If Database is a normal class, I just have to create two objects &#8211; get_db() only exists to make the usual case easy, and there&#8217;s no extra code in there that makes the unusual case hard (including unit-tests that may involve Database construction/destruction).</p>
<p>Check out <a href="http://c2.com/cgi/wiki?SingletonsAreEvi" rel="nofollow">http://c2.com/cgi/wiki?SingletonsAreEvi</a>  for a discussion on the topic (that wiki is a black hole for free time, seriously).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stut</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-6591</link>
		<dc:creator>Stut</dc:creator>
		<pubDate>Tue, 21 Oct 2008 09:22:28 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-6591</guid>
		<description>Thanks for your comment Tom.

I don&#039;t understand how using a singleton database class is more restrictive than your get_db() function when it comes to creating arbitrary connections. In addition whatever pattern you use can be written with any amount of flexibility you desire. I use a singleton class because it contains a number of helper methods to make certain database operations easier, but it also provides access to the raw resource if required. It also supports multiple connections (read, write, stats) but that&#039;s neither here nor there.

I also don&#039;t see why the limited life of objects in a PHP request is a factor? I prefer to write well-engineered code regardless of how long each instance will exist. I also prefer maintainable and reusable code, and singletons help me towards those goals in certain situations.

In regard to not trusting downstream devs, how exactly is a singleton different in that respect from your get_db() function? Aside from that it&#039;s not an issue of trust, it&#039;s a question of the right tool for the job. Singletons are as much about minimising resource usage as they are about anything else. They aid me in creating self-documenting code such that users of my class don&#039;t need a lesson before being able to use it properly.

But uhh... not that I want to discourage you - I like getting comments. And I&#039;m glad you&#039;re liking these snippets ;).</description>
		<content:encoded><![CDATA[<p>Thanks for your comment Tom.</p>
<p>I don&#8217;t understand how using a singleton database class is more restrictive than your get_db() function when it comes to creating arbitrary connections. In addition whatever pattern you use can be written with any amount of flexibility you desire. I use a singleton class because it contains a number of helper methods to make certain database operations easier, but it also provides access to the raw resource if required. It also supports multiple connections (read, write, stats) but that&#8217;s neither here nor there.</p>
<p>I also don&#8217;t see why the limited life of objects in a PHP request is a factor? I prefer to write well-engineered code regardless of how long each instance will exist. I also prefer maintainable and reusable code, and singletons help me towards those goals in certain situations.</p>
<p>In regard to not trusting downstream devs, how exactly is a singleton different in that respect from your get_db() function? Aside from that it&#8217;s not an issue of trust, it&#8217;s a question of the right tool for the job. Singletons are as much about minimising resource usage as they are about anything else. They aid me in creating self-documenting code such that users of my class don&#8217;t need a lesson before being able to use it properly.</p>
<p>But uhh&#8230; not that I want to discourage you &#8211; I like getting comments. And I&#8217;m glad you&#8217;re liking these snippets <img src='http://stut.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Barta</title>
		<link>http://stut.net/2008/10/20/snippet-singletons-with-php/comment-page-1/#comment-6584</link>
		<dc:creator>Tom Barta</dc:creator>
		<pubDate>Tue, 21 Oct 2008 02:26:18 +0000</pubDate>
		<guid isPermaLink="false">http://stut.net/blog/?p=263#comment-6584</guid>
		<description>I rarely ever found myself using &quot;true&quot; singletons in PHP - in most cases, there really isn&#039;t enough of a need to justify the effort.

You want a single database connection?  Make a function called get_db() that returns a local static Database object.  That&#039;s better than forcing Database to be a singleton, because it provides additional flexibility (ability to create arbitrary connections, should the need arise) with no obvious drawbacks.

This is particularly true in a language like PHP, where the scope of any object is at most for a single request.

In most cases I&#039;ve seen, the desire for a software designer to limit the number of instances of an object boils down to being a control issue, and a mistrust in the discipline of downstream developers.  I consider this an issue to solve with education, rather than jumping through hoops in the code (KISS principle).

But uhh... not that I want to discourage you - I like the list of PHP snippets.</description>
		<content:encoded><![CDATA[<p>I rarely ever found myself using &#8220;true&#8221; singletons in PHP &#8211; in most cases, there really isn&#8217;t enough of a need to justify the effort.</p>
<p>You want a single database connection?  Make a function called get_db() that returns a local static Database object.  That&#8217;s better than forcing Database to be a singleton, because it provides additional flexibility (ability to create arbitrary connections, should the need arise) with no obvious drawbacks.</p>
<p>This is particularly true in a language like PHP, where the scope of any object is at most for a single request.</p>
<p>In most cases I&#8217;ve seen, the desire for a software designer to limit the number of instances of an object boils down to being a control issue, and a mistrust in the discipline of downstream developers.  I consider this an issue to solve with education, rather than jumping through hoops in the code (KISS principle).</p>
<p>But uhh&#8230; not that I want to discourage you &#8211; I like the list of PHP snippets.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
