<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	>
<channel>
	<title>Comments on: Static methods vs singletons: choose neither</title>
	<atom:link href="http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/</link>
	<description>PHP Conference, PHP Training, PHP Consulting, PHP Magazine — php&#124;architect</description>
	<lastBuildDate>Tue, 07 Sep 2010 15:15:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Donovan Walker</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-4928</link>
		<dc:creator>Donovan Walker</dc:creator>
		<pubDate>Fri, 25 Jun 2010 02:50:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-4928</guid>
		<description>This is a wonderful post, but I&#039;d say it goes a bit far in it&#039;s almost universal condemnation of Static methods and singletons. Your comment about &#039;global scope&#039; may infer a frequent abuse of those programming patterns, but with responsible, restrained use in the correct contexts they can be successful, even desirable aspects of a solution.</description>
		<content:encoded><![CDATA[<p>This is a wonderful post, but I&#8217;d say it goes a bit far in it&#8217;s almost universal condemnation of Static methods and singletons. Your comment about &#8216;global scope&#8217; may infer a frequent abuse of those programming patterns, but with responsible, restrained use in the correct contexts they can be successful, even desirable aspects of a solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: just</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-3921</link>
		<dc:creator>just</dc:creator>
		<pubDate>Mon, 07 Jun 2010 23:53:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-3921</guid>
		<description>Here&#039;s how I test, I load the page in the browser and bam. It either works or it don&#039;t. Guess what, PHP will also tell me the problem. If you want to develop desktop Apps then go into desktop world. Every page request should not have to go through an oop  system. PHP is not an oop environment like desktop and does not benefit  because all those fancy objects don&#039;t stay in memory for the next request. While total oop does not make sense in PHP, it makes perfect sense in JavaScript because JavaScript objects stay in memory and are reused after the page is loaded.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s how I test, I load the page in the browser and bam. It either works or it don&#8217;t. Guess what, PHP will also tell me the problem. If you want to develop desktop Apps then go into desktop world. Every page request should not have to go through an oop  system. PHP is not an oop environment like desktop and does not benefit  because all those fancy objects don&#8217;t stay in memory for the next request. While total oop does not make sense in PHP, it makes perfect sense in JavaScript because JavaScript objects stay in memory and are reused after the page is loaded.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Weaver</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-566</link>
		<dc:creator>Ryan Weaver</dc:creator>
		<pubDate>Sun, 14 Mar 2010 02:14:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-566</guid>
		<description>@Doug Boude
Well, you bring up a fine point between injecting a dependency and retrieving a singleton statically. What you quoted me on is taken closely from a comment made by Fabien in the above-referenced blog post. My definition of a singleton would be something that is retrieved via a static getInstance() type of call. You may have some resources that are only actually created once, but then are passed into your scope. It&#039;s a subtle difference and what you gain or lose from one approach to the other may be subtle as well.</description>
		<content:encoded><![CDATA[<p>@Doug Boude<br />
Well, you bring up a fine point between injecting a dependency and retrieving a singleton statically. What you quoted me on is taken closely from a comment made by Fabien in the above-referenced blog post. My definition of a singleton would be something that is retrieved via a static getInstance() type of call. You may have some resources that are only actually created once, but then are passed into your scope. It&#8217;s a subtle difference and what you gain or lose from one approach to the other may be subtle as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-503</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 12 Mar 2010 12:20:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-503</guid>
		<description>I&#039;m all for using the right tool for the right job, but maybe I could be doing something &#039;better&#039;.

My main singleton class holds multiple instances of the PDO object (for multiple databases) - the idea being that I have just one instance of each connection class.

When wring my data management objects, these objects require one or more of the db connections. Passing these connections in the cobstructor would require the object invoking the data management object to have an instance of the connection ready - so where would that instance come from?

As for static methods I jsut use this so non-object fucntions can be called using the __autoload() function - maybe this is wrong too, but it suits me very well ;)</description>
		<content:encoded><![CDATA[<p>I&#8217;m all for using the right tool for the right job, but maybe I could be doing something &#8216;better&#8217;.</p>
<p>My main singleton class holds multiple instances of the PDO object (for multiple databases) &#8211; the idea being that I have just one instance of each connection class.</p>
<p>When wring my data management objects, these objects require one or more of the db connections. Passing these connections in the cobstructor would require the object invoking the data management object to have an instance of the connection ready &#8211; so where would that instance come from?</p>
<p>As for static methods I jsut use this so non-object fucntions can be called using the __autoload() function &#8211; maybe this is wrong too, but it suits me very well ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Boude</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-446</link>
		<dc:creator>Doug Boude</dc:creator>
		<pubDate>Thu, 11 Mar 2010 15:27:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-446</guid>
		<description>Ryan, I have a question about something you said: &quot;the service container, while not technically a singleton, is constructed just once and then is available where needed&quot;. My understanding of what a Singleton is has always been that is constructed just once and then is available where needed. It appears that you are saying that is *not* the definition, based on your statement about the service container. Can you elaborate for me?</description>
		<content:encoded><![CDATA[<p>Ryan, I have a question about something you said: &#8220;the service container, while not technically a singleton, is constructed just once and then is available where needed&#8221;. My understanding of what a Singleton is has always been that is constructed just once and then is available where needed. It appears that you are saying that is *not* the definition, based on your statement about the service container. Can you elaborate for me?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Weaver</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-377</link>
		<dc:creator>Ryan Weaver</dc:creator>
		<pubDate>Thu, 11 Mar 2010 02:15:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-377</guid>
		<description>Thanks for the comments - there are some really in-depth views here that add a lot of value. Let me address a few:

@Matt Butcher (on a logger)
Definitely a valid point, and a difficult one to solve. Certainly you don&#039;t want to pass a dependency like a logger to everything in your application. That being said, you&#039;ll also want to avoid hardcoding your logger while not overly-depending on the global state (because I like being able to use my classes independent of everything else). Most any solution here requires some sort of globally-available context.

@cease (on checking on DI in the spring framework)
I agree that singletons will still exist and should in some cases. After all, we ARE responding to one request: there&#039;s something inherently &quot;single&quot; about that. In symfony 2, the service container, while not technically a singleton, is constructed just once and then is available where needed. The spring framework is precisely the model that most new php paradigms are following. In other words, my implementation probably wouldn&#039;t be any different - spring is the model case. There&#039;s some conversation about this very topic (specifically in the comments) here: http://fabien.potencier.org/article/14/symfony-service-container-using-a-builder-to-create-services

@Nate Abele (on nothing inherently bad with static methods, singletons, mutable global states, and referential transparency)
No, there&#039;s nothing inherently wrong with them - you just have to realize what you aim to lose by using them. Namely, your class will have some sort of hardcoded dependency and may be difficult to use in isolation (e.g. testing) because the class looks for some global condition that may not be available. If you&#039;re cool with that (and in a lot of cases you may be) then they&#039;re fine.

Regarding the true problem being with mutable global states, that certainly makes sense. Since immutable globals are essentially constants, if a global constant is really what you&#039;re after, then that&#039;s fine. As an extreme, I certainly won&#039;t stop using real constants (myClass::SOME_VALUE) any time soon.

On the referential transparency front - you&#039;re right again. Hopefully with DI, at the very least, you&#039;ll be less surprised when something proves opaque since scope is clearer. That being said, if your nesting level is deep (the objects in your scope have many objects in their scope), then you may still lose sight of the full &quot;scope&quot; of your actions.

Thanks for the comments!</description>
		<content:encoded><![CDATA[<p>Thanks for the comments &#8211; there are some really in-depth views here that add a lot of value. Let me address a few:</p>
<p>@Matt Butcher (on a logger)<br />
Definitely a valid point, and a difficult one to solve. Certainly you don&#8217;t want to pass a dependency like a logger to everything in your application. That being said, you&#8217;ll also want to avoid hardcoding your logger while not overly-depending on the global state (because I like being able to use my classes independent of everything else). Most any solution here requires some sort of globally-available context.</p>
<p>@cease (on checking on DI in the spring framework)<br />
I agree that singletons will still exist and should in some cases. After all, we ARE responding to one request: there&#8217;s something inherently &#8220;single&#8221; about that. In symfony 2, the service container, while not technically a singleton, is constructed just once and then is available where needed. The spring framework is precisely the model that most new php paradigms are following. In other words, my implementation probably wouldn&#8217;t be any different &#8211; spring is the model case. There&#8217;s some conversation about this very topic (specifically in the comments) here: <a href="http://fabien.potencier.org/article/14/symfony-service-container-using-a-builder-to-create-services" rel="nofollow">http://fabien.potencier.org/article/14/symfony-service-container-using-a-builder-to-create-services</a></p>
<p>@Nate Abele (on nothing inherently bad with static methods, singletons, mutable global states, and referential transparency)<br />
No, there&#8217;s nothing inherently wrong with them &#8211; you just have to realize what you aim to lose by using them. Namely, your class will have some sort of hardcoded dependency and may be difficult to use in isolation (e.g. testing) because the class looks for some global condition that may not be available. If you&#8217;re cool with that (and in a lot of cases you may be) then they&#8217;re fine.</p>
<p>Regarding the true problem being with mutable global states, that certainly makes sense. Since immutable globals are essentially constants, if a global constant is really what you&#8217;re after, then that&#8217;s fine. As an extreme, I certainly won&#8217;t stop using real constants (myClass::SOME_VALUE) any time soon.</p>
<p>On the referential transparency front &#8211; you&#8217;re right again. Hopefully with DI, at the very least, you&#8217;ll be less surprised when something proves opaque since scope is clearer. That being said, if your nesting level is deep (the objects in your scope have many objects in their scope), then you may still lose sight of the full &#8220;scope&#8221; of your actions.</p>
<p>Thanks for the comments!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tibo Beijen</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-299</link>
		<dc:creator>Tibo Beijen</dc:creator>
		<pubDate>Tue, 09 Mar 2010 16:50:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-299</guid>
		<description>Regarding the benchmarking: I smell... premature optimization.

While both singletons and static method calls are best avoided (generally speaking) I dislike static calls much more than singletons: A singleton has a getInstance() method of which a setInstance() counterpart can exist. Thereby it is possible to alter the getInstance behaviour to return an object of a different type (Ok, so it&#039;s not a singleton anymore but then again, it shouldn&#039;t have been in the first place). And if the application code doesn&#039;t only accept instances of the singleton class but accepts an interface one doesn&#039;t neccessarily have to resort to subclasses of the singleton class for the returned object...</description>
		<content:encoded><![CDATA[<p>Regarding the benchmarking: I smell&#8230; premature optimization.</p>
<p>While both singletons and static method calls are best avoided (generally speaking) I dislike static calls much more than singletons: A singleton has a getInstance() method of which a setInstance() counterpart can exist. Thereby it is possible to alter the getInstance behaviour to return an object of a different type (Ok, so it&#8217;s not a singleton anymore but then again, it shouldn&#8217;t have been in the first place). And if the application code doesn&#8217;t only accept instances of the singleton class but accepts an interface one doesn&#8217;t neccessarily have to resort to subclasses of the singleton class for the returned object&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Abele</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-298</link>
		<dc:creator>Nate Abele</dc:creator>
		<pubDate>Tue, 09 Mar 2010 16:40:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-298</guid>
		<description>I guess maybe this is the kind of thing that passes for &quot;best practice&quot; in PHP, but there are a whole host of concepts that apparently no one developing in PHP (or at least writing about developing in PHP) are aware of.

First, let&#039;s be perfectly clear: there is nothing inherently wrong with static methods. Nor, in fact, is there anything inherently wrong with singletons.

The real problems are (mutable) global state and referential transparency. Things that rely on global state are mainly only problematic when that state is mutable. And DI doesn&#039;t solve the problem of referential transparency at all, it just treats the symptoms, making it easier to live with.

Instead of spouting off about high-level design patterns, it&#039;s better to learn about the underlying programming concepts that those patterns try to implement or solve. Then you can actually have a productive discussion.</description>
		<content:encoded><![CDATA[<p>I guess maybe this is the kind of thing that passes for &#8220;best practice&#8221; in PHP, but there are a whole host of concepts that apparently no one developing in PHP (or at least writing about developing in PHP) are aware of.</p>
<p>First, let&#8217;s be perfectly clear: there is nothing inherently wrong with static methods. Nor, in fact, is there anything inherently wrong with singletons.</p>
<p>The real problems are (mutable) global state and referential transparency. Things that rely on global state are mainly only problematic when that state is mutable. And DI doesn&#8217;t solve the problem of referential transparency at all, it just treats the symptoms, making it easier to live with.</p>
<p>Instead of spouting off about high-level design patterns, it&#8217;s better to learn about the underlying programming concepts that those patterns try to implement or solve. Then you can actually have a productive discussion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: php&#124;architect: Static methods vs singletons: choose neither &#124; Webs Developer</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-296</link>
		<dc:creator>php&#124;architect: Static methods vs singletons: choose neither &#124; Webs Developer</dc:creator>
		<pubDate>Tue, 09 Mar 2010 16:04:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-296</guid>
		<description>[...] versus static methods debate that seems to com up every once and a while with a better suggestion - dependency injection.   Much more important than performance is the fact that both static methods and singletons suffer [...]</description>
		<content:encoded><![CDATA[<p>[...] versus static methods debate that seems to com up every once and a while with a better suggestion &#8211; dependency injection.   Much more important than performance is the fact that both static methods and singletons suffer [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cease</title>
		<link>http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/comment-page-1/#comment-276</link>
		<dc:creator>cease</dc:creator>
		<pubDate>Mon, 08 Mar 2010 21:57:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.phparch.com/?p=4179#comment-276</guid>
		<description>@Matt you use aop for loggers.. cross cutting concern.

@author I suggest you still look at the spring implementation of DI.  The use of singletons are used within their implementation although it is hidden from end users. The applicationContext initializes beans as singletons that are injected.   Would like to hear how you&#039;re implementation will be different.</description>
		<content:encoded><![CDATA[<p>@Matt you use aop for loggers.. cross cutting concern.</p>
<p>@author I suggest you still look at the spring implementation of DI.  The use of singletons are used within their implementation although it is hidden from end users. The applicationContext initializes beans as singletons that are injected.   Would like to hear how you&#8217;re implementation will be different.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc (user agent is rejected)
Database Caching 1/29 queries in 0.032 seconds using apc

Served from: www.phparch.com @ 2010-09-10 15:53:22 -->