<?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: PHP Dependency Injection</title>
	<atom:link href="http://www.potstuck.com/2009/01/08/php-dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/</link>
	<description></description>
	<lastBuildDate>Fri, 23 Dec 2011 14:29:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: chris holland</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-438</link>
		<dc:creator>chris holland</dc:creator>
		<pubDate>Tue, 05 Jul 2011 17:20:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-438</guid>
		<description>Great article, many thanks for writing it.

Thought: I agree with you that passing numerous arguments to a constructor can get unwieldy, and that using a setter gives you a cleaner approach.

However, one might make the constructor more elegant to deal with. Instead of passing numerous arguments to the constructor, you might:

* define an BookConfiguration Interface with various possible classes that implement it:
** AppBookConfiguration
** UnitTestBookConfiguration

* have ONE constructor on your Book class which expects ONE argument of type &quot;BookConfiguration&quot; (one little-used feature of PHP 5 is you can actually type your arguments like you can in Java and other languages):

class Book {
 
	private $_databaseConnection;
 
	public function __construct(BookConfiguration $configuration) {
              //BookConfiguration is an Interface. when you invoke the constructor,
              //you&#039;ll actually be passing instances of either &quot;AppBookConfiguration&quot;
              //or UnitTestBookConfiguration which are concrete classes that implement
             // the BookConfiguration interface.

                 $this-&gt;_databaseConnection = $configuration-&gt;getDatabaseConnection()

        }//Book Class Constructor
  
}//Book Class

A few advantages:

* addresses the possible constructor mess you outlined
* as you extend Book, and Book &quot;requires more things to work&quot;, you can simply still just extend your configuration class
* preserves dependency injection and the ability to supply alternate configurations based on the environment in which the class is intantiated.
* doesn&#039;t require an extra container class to perform bootstrapping operations that are, in essence, still vital to the proper operation of your  Book class.</description>
		<content:encoded><![CDATA[<p>Great article, many thanks for writing it.</p>
<p>Thought: I agree with you that passing numerous arguments to a constructor can get unwieldy, and that using a setter gives you a cleaner approach.</p>
<p>However, one might make the constructor more elegant to deal with. Instead of passing numerous arguments to the constructor, you might:</p>
<p>* define an BookConfiguration Interface with various possible classes that implement it:<br />
** AppBookConfiguration<br />
** UnitTestBookConfiguration</p>
<p>* have ONE constructor on your Book class which expects ONE argument of type &#8220;BookConfiguration&#8221; (one little-used feature of PHP 5 is you can actually type your arguments like you can in Java and other languages):</p>
<p>class Book {</p>
<p>	private $_databaseConnection;</p>
<p>	public function __construct(BookConfiguration $configuration) {<br />
              //BookConfiguration is an Interface. when you invoke the constructor,<br />
              //you&#8217;ll actually be passing instances of either &#8220;AppBookConfiguration&#8221;<br />
              //or UnitTestBookConfiguration which are concrete classes that implement<br />
             // the BookConfiguration interface.</p>
<p>                 $this-&gt;_databaseConnection = $configuration-&gt;getDatabaseConnection()</p>
<p>        }//Book Class Constructor</p>
<p>}//Book Class</p>
<p>A few advantages:</p>
<p>* addresses the possible constructor mess you outlined<br />
* as you extend Book, and Book &#8220;requires more things to work&#8221;, you can simply still just extend your configuration class<br />
* preserves dependency injection and the ability to supply alternate configurations based on the environment in which the class is intantiated.<br />
* doesn&#8217;t require an extra container class to perform bootstrapping operations that are, in essence, still vital to the proper operation of your  Book class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 25cm</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-437</link>
		<dc:creator>25cm</dc:creator>
		<pubDate>Mon, 20 Jun 2011 05:01:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-437</guid>
		<description>Still usefull article, thx</description>
		<content:encoded><![CDATA[<p>Still usefull article, thx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-405</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Mon, 28 Mar 2011 09:40:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-405</guid>
		<description>presario, thank you, updated.</description>
		<content:encoded><![CDATA[<p>presario, thank you, updated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: presario</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-404</link>
		<dc:creator>presario</dc:creator>
		<pubDate>Thu, 24 Mar 2011 16:15:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-404</guid>
		<description>Please change &quot;now a days&quot; to &quot;nowadays&quot;.</description>
		<content:encoded><![CDATA[<p>Please change &#8220;now a days&#8221; to &#8220;nowadays&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: presario</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-403</link>
		<dc:creator>presario</dc:creator>
		<pubDate>Thu, 24 Mar 2011 16:13:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-403</guid>
		<description>Ryan, you have done a very good job! Very nice article! Thank you!</description>
		<content:encoded><![CDATA[<p>Ryan, you have done a very good job! Very nice article! Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-401</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Mon, 14 Feb 2011 10:55:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-401</guid>
		<description>Tiddo, it&#039;s fine to use a singleton for creation, because the class/object you are creating is not dependent on that singleton.</description>
		<content:encoded><![CDATA[<p>Tiddo, it&#8217;s fine to use a singleton for creation, because the class/object you are creating is not dependent on that singleton.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jsonx</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-397</link>
		<dc:creator>jsonx</dc:creator>
		<pubDate>Thu, 10 Feb 2011 10:12:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-397</guid>
		<description>Thank you. This is the best explanation of Dependency Injection i have ever seen.</description>
		<content:encoded><![CDATA[<p>Thank you. This is the best explanation of Dependency Injection i have ever seen.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tiddo</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-395</link>
		<dc:creator>Tiddo</dc:creator>
		<pubDate>Fri, 04 Feb 2011 17:49:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-395</guid>
		<description>Very nice article. However, I have just one question about this: you said singleton patterns are bad, and I think I can agree with you. However, I think global variables and pure static classes are even worse than singleton patterns. Using this design pattern, you&#039;ll have to use either pure static classes, as in your container example, or you&#039;ll have to use global variables for e.g. the database connection. Is there any way around this?</description>
		<content:encoded><![CDATA[<p>Very nice article. However, I have just one question about this: you said singleton patterns are bad, and I think I can agree with you. However, I think global variables and pure static classes are even worse than singleton patterns. Using this design pattern, you&#8217;ll have to use either pure static classes, as in your container example, or you&#8217;ll have to use global variables for e.g. the database connection. Is there any way around this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pooyan</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-303</link>
		<dc:creator>Pooyan</dc:creator>
		<pubDate>Tue, 12 Oct 2010 16:53:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-303</guid>
		<description>I googled for Dependency Injection a lot. I have to say that this article does the best description of Dependency Injection in its simplest, yet most effective manner. Thanks to the author of this article. Keep up the good job.</description>
		<content:encoded><![CDATA[<p>I googled for Dependency Injection a lot. I have to say that this article does the best description of Dependency Injection in its simplest, yet most effective manner. Thanks to the author of this article. Keep up the good job.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.potstuck.com/2009/01/08/php-dependency-injection/comment-page-1/#comment-300</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Mon, 11 Oct 2010 20:03:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.potstuck.com/?p=66#comment-300</guid>
		<description>@ Timido: Excellent point.  How do you integrate DI with 3rd party frameworks that do their own object creation?  Most of these frameworks will allow you to register plugins to interact/control object creation.  Lets stick with your ORM example... Doctrine (the best php orm, by far) allows you to register listeners/hydrators.  With these, you are allowed to do anything to the objects after the ORM creates them (like inject all of the dependencies!).  Just about every good framework provides a way of doing this.</description>
		<content:encoded><![CDATA[<p>@ Timido: Excellent point.  How do you integrate DI with 3rd party frameworks that do their own object creation?  Most of these frameworks will allow you to register plugins to interact/control object creation.  Lets stick with your ORM example&#8230; Doctrine (the best php orm, by far) allows you to register listeners/hydrators.  With these, you are allowed to do anything to the objects after the ORM creates them (like inject all of the dependencies!).  Just about every good framework provides a way of doing this.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.potstuck.com @ 2012-02-05 15:01:24 -->
