<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/">

    <channel>
    
    <title>The Pope Is Dead</title>
    <link>http://www.thepopeisdead.com/</link>
    <description>Just another web development blog...</description>
    <dc:language>en</dc:language>
    <dc:creator>mute.pop3@gmail.com</dc:creator>
    <dc:rights>Copyright 2011</dc:rights>
    <dc:date>2011-11-13T13:40:00+00:00</dc:date>
    <admin:generatorAgent rdf:resource="http://expressionengine.com/" />
    

    <item>
      <title>Moving On From The Storefront Model Design</title>
      <link>http://www.thepopeisdead.com/main/comments/moving_on_from_the_storefront_model_design/</link>
      <guid>http://www.thepopeisdead.com/main/comments/moving_on_from_the_storefront_model_design/#When:13:40:00Z</guid>
      <description><![CDATA[<p>Its coming up to nearly two years since my book was released, so I thought it was about time I did some blogging again.</p>
<p>Over the past couple of years I have spoken to various people who have read my book and many have liked the way I implemented my Models for the Storefront, obviously looking at the design now I can see many design mistakes, some of which I did actually mention in the book. A lot of people also want to use my design in their applications and they occasionally email/tweet me to ask things, what I usually end up saying to them is the design was very focused on being an example for the book and a such its probably not what I would consider production ready code, I then normally advise them to checkout Doctrine 2 and consider splitting read and write so the Models become write only (more on this later).</p>
<p>With this in mind I have decided to go back and refactor part of the Storefront model, to start we can look at some of the guides we can use to improve the design.</p>
<h4>Modelling Behaviour</h4>
<p>One of the most important things that is missed by many examples on Zend Framework models and Doctrine is that the models usually contain little or no behaviour. When we have models like this we are usually looking at the Anemic Domain Model anti-pattern. Whilst this probably does not apply so greatly if we are not using the Domain Model pattern we can still learn some valuable lessons from it. By modelling behaviour we should end up with a simpler and more lean model, this also helps when we come change the model as each behaviour is distinct.</p>
<p>We can identify models that do not implement behaviour fairly easily by looking for the following factors:</p>
<ul style="margin-top:0; margin-bottom:0;">
<li>Lots of Getters &amp; Setters</li>
<li>Model methods have no "Verbs" in them</li>
<li>Behaviour is found somewhere else, the model is used or wrapped to provide behaviour</li>
<li>Unit tests test the data structure of the model </li>
</ul>
<h4>Tell Don't Ask</h4>
<p>Tell Don't Ask is a style of Object Oriented Programming that states that an object should only ever tell another object to do something, it should never ask an object for its state. The reason for this is nicely described by Alec Sharp in Smalltalk by Example where he states:</p>
<p>"Procedural code gets information then makes decisions. Object-oriented code tells objects to do things."</p>
<p>So by using Tell Don't Ask it forces us to "do things" and means we have no getters or setters in our model. This may seem like a strange concept to start, as I know you are probably wondering how we display information which I will cover shortly. The main advantages here are going to be that we have a truly OO model that has proper encapsulation, it also means that we don't have lots of other code that makes decisions based on the state of an object. Not using Tell Don't Ask, we usually end up with a rich service layer that contains the actual behaviour, this then means we have models that only contain data and getters/setters, and this in turn leads us again to an Anemic Domain Model.</p>
<h4>Separating Read &amp; Write</h4>
<p>Another thing that can cause us problems is mixing the Domain objects with the View (Read). If we do this we generally end up compromising our model to support various display problems, we can also get long object chains like $user-&gt;getClient()-&gt;getAddress()-&gt;getPostCode(). This will also lead us to violate encapsulation and forces us to mix display and behaviour, by doing this we cannot adequately address either problem. Also in PHP we have problems with its share nothing architecture, unlike some other languages (Java, .NET) PHP cannot store object beyond a request, this means that we have to reload our objects every request, whereas other languages can use object between multiple requests. This is not a fault within the PHP language just a fact that we need to be aware of when dealing with complex object models, indeed in other languages you have to think about locking etc much more so there is a payoff either way.</p>
<p>We also need to consider the use of an ORM (like Doctrine 2) here, now ORM's provide us with an elegant toolset for persisting the state of our objects, however they do not answer all our problems! When using an ORM you will undoubtably at some point need to display a few thousand/million "things" at the same time, say for an export of orders. Now what we find here is that we cannot use objects as we know we will run out of memory, therefore we end up side stepping the ORM, we choose to use things like Native Queries (Doctrine) or create a service that gets the data straight from the database. This is all very valid but if we didn't mix display and behaviour we wouldn't get this problem.</p>
<p>Database design is also key here, just like when the design our model, the database is designed to serve a particular task, in the case of our models this task is to persist the state of our domain. Now ask yourself the questions is this the same problem as pulling reports from the database or displaying information to the user? I would say probably not, to display a report for example we may need summary fields or calculated totals for performance, we cannot create a one size fits all database, especially if we need high performance.</p>
<h4>Refactoring the Storefront Catalog</h4>
<p>Right now time for some code, in this section we will go through and refactor a small part of the Storefront model and try to apply the guides above to it. For this we will keep the model really simple so we don't get over involved in the implementation, so our basic spec is that we can create products and products have a name, category and price, we can also update the product name.</p>
<p>You can find the code for this here: <a href="https://github.com/muteor/Examples/tree/storefrontModelPart1">https://github.com/muteor/Examples/tree/storefrontModelPart1</a></p>
<p>First a test…..</p>
<script src="https://gist.github.com/1348264.js?file=productTest.php"></script>
<p>Here we have the test-case for our basic product model which contains two tests. The first test is pretty normal and simply tests the creation of a new Product, notice that we have no getters here, this is why we are using PHPUnit's readAttribute method to read the protected properties. Another notable aspect is that we are using the constructor in the way it is meant, we intend to not have any base class that implements the construct, this means that whenever we do new Product we will be creating a brand new Product. We also use the constructor to validate our object, by using the language feature we can make sure that a new product is valid by adding the requirements into the constructor (its parameters).</p>
<p>The second test, tests the process of updating the products name, here we see that to do the update we do not use a setter. Instead we introduce the concept of a Command, a Command is applied to a model and the model processes it to complete the state modification. By doing this we are now only communicating to our domain in objects, this means we have a high level of encapsulation and separation.</p>
<p>The idea to use Commands is partly because we want a Tell Don't Ask style system, however this is also related to the <a href="http://en.wikipedia.org/wiki/Command-query_separation">Command-Query-Separation</a> (CQS) pattern, this states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. This gives us clear markers to what methods make state changes and which ones don't, though in our models we are not going to have anything that returns state.</p>
<p>If we now look at the our model classes:</p>
<script src="https://gist.github.com/1348264.js?file=Product.php"></script>
<p>Looking at the Product construct we see that it takes three parameters, these are what we deem to create a valid Product. In a real world situation we would probably have many more properties that are required to make a valid Product, in this case we can sometimes find we have long parameter lists, to mitigate this consider using <a href="http://refactoring.com/catalog/introduceParameterObject.html">Parameter Objects</a>. If we look at the body of the constructor we see that it creates the CreateProduct Command and applies it to the object via handle(). Now the new call in the constructor would normally ring alarm bells for me as new is always problematic for testing, however in this case I treat the Commands as an integral part of the model and test these along with the model, this is essentially because a Command should be a very simple <a href="http://en.wikipedia.org/wiki/Data_transfer_object">DTO</a> that we can control easily in our tests. The handle() method simply takes the class name of the Command and looks the corresponding handler, so for createProduct this is the onCreateProduct() method.</p>
<p>The onCreateProduct() method takes the command and applies the data contained in the command to the Product, all very simply really, though notice we use a protected method so everything is applied via handle(). Similarly in the OnUpdateName() method the handler simply takes the command data and applies it to the Product, obviously these are fairly simple examples we could have validation etc in our handlers that would throw various exceptions for us.</p>
<p>Finally we should look at the Commands, they should hopefully be pretty self explanatory.</p>
<script src="https://gist.github.com/1348264.js?file=AbstractCommand.php"></script>
<script src="https://gist.github.com/1348264.js?file=CreateProduct.php"></script>
<script src="https://gist.github.com/1348264.js?file=UpdateName.php"></script>
<p>Looking at the AbstractCommand class we see that it uses the PHP magic __get() method, we use this to enforce a read-only object so that the Command cannot be changed once it has been created. Both the the concrete Command implementations simply extend the AbstractCommand and define the protected properties that make up the Command, all properties are passed in the construct of the Command.</p>
<p>Wrapping up we can see we have a simple design and nice separation of concerns going on IMHO. Now working like this can be kept simple though if we face complex domains we can also consider using Domain Driven Design (DDD), DDD provides us with a few more tools to guide us in our modelling practices. I won't go into too much detail but if you are interested the two most important ones when working like this are <a href="http://en.wikipedia.org/wiki/Domain-driven_design#Bounded_context">Bounded Context</a> and <a href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Aggregate Roots</a>, though also checkout <a href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Entities</a> and <a href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Value Objects</a>.</p>
<h4>Persistance</h4>
<p>Once we have our Domain modelled (and tested) we need to persist its state, for this I would suggest using Doctrine 2, it is in my opinion a great ORM for PHP. I will be rewriting the Storefront example using it when I get some time (and when ZF2 is more stable), so you will be able to see some solid examples then, until that time the Doctrine documentation and examples online should get you started.</p>
<h4>Creating a Read Model</h4>
<p>So far we have no way of reading state from our models, as we have no getters. We could simply add these and use our models in the view, however I have found that this becomes heavy and has the potential to hinder performance, therefore we can create a Read Model. A Read Model can be as simple or complex as you like what we want to do though is balance our needs for performance and maintainability. We could simply have service classes that contain a range of methods that query the database directly or we could go a little more complex and implement some sort of service that returns simple DTO's of our objects.</p>
<p>What I have been using is Doctrine's query interface and instead of returning the mapped entities returning array or scalar values. Using this I get all the relationships handled nicely by Doctrine making it really easy to query my Domain. In cases where this is not performant I use Native Queries or use the DBAL directly.</p>
<p>Here is my base Read Model class:</p>
<script src="https://gist.github.com/1348264.js?file=AbstractReadModel.php"></script>
<p>The base ReadModel class simply wraps the doctrine query interface and provides as set of convenience methods for returning the queried data in various formats. We can then extend this class to implement domain specific querying, for example we would probably have a Product Read Model that would return products by categories etc. This could look something like this:</p>
<script src="https://gist.github.com/1348264.js?file=gistfile1.aw"></script>
<h4>Going Even Further</h4>
<p>Time to come clean, what I have been going through here is an ultra simplified version of a CQRS system by using some of the core concepts behind it. CQRS or Command-Query-Responsibility-Segregation is an architectural pattern/methodology that can be used for highly scalable distributed systems. The principles behind it are fairly simple though the implementation can be complex. If you would like to know more I would suggest starting <a href="http://cqrs.wordpress.com/documents/cqrs-introduction/">here</a>, this article gives a great introduction. Obviously CQRS is a very high level pattern and most of the time we do not need the type of performance etc that it provides, but I believe we can learn some valuable lessons from the concepts that drive it, also by using what I have shown here you also have the option of refactoring into a more CQRS like system in the future.</p>
<h4>Wrap Up</h4>
<p>So this article represents my current view on modelling in PHP, this is all pretty much the bleeding edge of my knowledge so I would be interested in hearing views on this approach and if I am missing anything major. So far I have had much success with using this approach, I find it keeps things simple as well as providing chances to scale the application further in the future. You do end up with a lot of Commands, though this does help when creating API's as everything is very discrete, also you can do nice things like queuing commands offline and applying them later which is handy for occasionally connected clients.</p>
<p>Thats all for now, enjoy!</p>
<p> </p>]]></description>
      <dc:subject>PHP, Web Development, Zend Framework</dc:subject>
      <dc:date>2011-11-13T13:40:00+00:00</dc:date>
    </item>

    <item>
      <title>New Zend_Application feature/update</title>
      <link>http://www.thepopeisdead.com/main/comments/new_zend_application_feature_update/</link>
      <guid>http://www.thepopeisdead.com/main/comments/new_zend_application_feature_update/#When:14:59:01Z</guid>
      <description><![CDATA[<p>
Just noticed this in the trunk of ZF, probably in 1.10 too. You do not now need to register the default resource autoloader for your default module, you can now simply put appnamespace = &#8220;myNamepase&#8221; in the app config. This will register the default resources for your default module.
<br />

</p>]]></description>
      <dc:subject>Zend Framework</dc:subject>
      <dc:date>2009-12-22T14:59:01+00:00</dc:date>
    </item>

    <item>
      <title>New Storefront Example Additions</title>
      <link>http://www.thepopeisdead.com/main/comments/new_storefront_example_additions/</link>
      <guid>http://www.thepopeisdead.com/main/comments/new_storefront_example_additions/#When:14:26:00Z</guid>
      <description><![CDATA[<p>
I have added search functionality to the Storefront example using Zend_Search_Lucene from the Zend Framework 1.8: Web Application Development book, I will hopefully get some time to write about this over christmas, for now though if you are interested you can download the code for the Google code sites trunk.
</p>
<p>
<a href="http://code.google.com/p/zendframeworkstorefront/">ZF Storefront Example on Google Code</a>
</p>
<p>
svn checkout <a href="http://zendframeworkstorefront.googlecode.com/svn/trunk/">http://zendframeworkstorefront.googlecode.com/svn/trunk/</a> zendframeworkstorefront-read-only
</p>
<p>Enjoy...</p>]]></description>
      <dc:subject>Zend Framework</dc:subject>
      <dc:date>2009-12-13T14:26:00+00:00</dc:date>
    </item>

    <item>
      <title>Dependency Injection</title>
      <link>http://www.thepopeisdead.com/main/comments/dependency_injection/</link>
      <guid>http://www.thepopeisdead.com/main/comments/dependency_injection/#When:21:10:01Z</guid>
      <description><![CDATA[<p>
    For a while now I have been looking at Dependency Injection in PHP and
    have been experimenting with the Yadif DI Container, after much thought I 
    decided to stop creating the article on ZF and Yadif as I now believe that 
    in PHP a DI Container is overkill. This article is therefore an explaination 
    of the general DI principles that we all can use to improve our code and 
    why DI Containers are overkill in PHP.
</p>

<h4>Dependency injection (the problem)</h4>
<p>
    Dependency Injection is a design pattern that deals with object
    configuration or object dependencies. By a dependency we mean that an
    Object uses another Object or Service, this brings us to the problem of
    how an Object handles it supporting services. The easiest way to look
    at this is through an example, consider the following class.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="kw2">new</span> dbConnection<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// other methods..</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.015 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    In class <em>Foo</em> we need to use the supporting database connection, to do
    this we instantiate a new <em>dbConnection</em> class in Foo’s constructor. This
    seems like a totally reasonable way to give our class access to the
    database connection, however by doing this we are giving <em>Foo</em> control
    over its own configuration (dependencies); this causes us the following
    problems.
</p>
<ul>
    <li>We cannot replace the database <em>dbConnection</em> instance when testing as we have no control over its instantiation.</li>
    <li><em>Foo</em> must know how to configure the database connection which breaks the encapsulation of our object.</li>
    <li>We cannot easily change the dependencies of <em>Foo</em>, to do so we must edit the code.</li>
</ul>
<p>
    So basically when we have a new call within our class we create a tight
    coupling between our objects. To solve this problem we need to
    configure the object from the outside rather than from within, we can
    do this using Dependency Injection which injects the dependencies into
    the object and takes control over the objects dependencies.
</p>

<h4>Types of dependency injection</h4>
<p>
    To start we will look at some basic types of Dependency Injection, we
    can easily use these techniques in any application without the need for
    a Dependency Injection framework, though we still use these techniques
    even with a framework.
</p>

<h5>Constructor based injection</h5>
<p>
    Constructor injection is where we inject our dependencies via the
    objects constructor method; if we now go back to the <em>Foo</em> example we can
    refactor that class to support constructor injection.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span>dbConnection <span class="re1">$db</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// other methods..</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.008 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    We can see that we have only had to make minor changes to the class to
    implement constructor injection. To do this we now pass the
    <em>dbConnection</em> class into <em>Foo</em> via a constructor parameter, meaning to use
    <em>Foo</em> we now do:
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="re1">$foo</span> <span class="sy0">=</span> <span class="kw2">new</span> Foo<span class="br0">&#40;</span><span class="kw2">new</span> dbConnection<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li></ol><div class="foot">Parsed in 0.004 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    The <em>dbConnection</em> class is now injected into <em>Foo</em>, meaning we can easily
    replace it and have removed the responsibility of instantiation from
    <em>Foo</em>.
</p>
<p>
    As we can see this is very simple and I am sure we have all already
    done similar things in our code but maybe without knowing. We can also
    further improve this by introducing an interface, currently the
    constructor parameter is type hinted as <em>dbConnection</em> meaning we can
    only pass in instances of <em>dbConnection</em> or subclasses of it.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">interface</span> IdbConnection</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="kw2">class</span> myDbConnection implements IdbConnection</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="kw2">class</span> testDbConnection implements IdbConnection</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span>IdbConnection <span class="re1">$db</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// other methods..</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.019 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    By introducing an interface for <em>dbConnection</em> we can now easily create
    many different types of <em>dbConnection</em> that <em>Foo</em> can use, <em>Foo</em> will know
    how to use each connection type as they will all share the same public
    interface. Generally this is good practice when injecting dependencies
    as it make our implementation much more flexible.
</p>

<h5>Setter based injection</h5>
<p>
    Setter based injection is just as simple as constructor based
    injection, however this time rather than providing our dependencies in
    the construct we provide them using a setter method.
</p>
<p>
    Going back to our example we can refactor <em>Foo</em> to use setter injection.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> setDb<span class="br0">&#40;</span>IdbConnection <span class="re1">$db</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$db</span><span class="sy0">;</span> &nbsp; &nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getDb<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db<span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// other methods..</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.012 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    We can see this is a fairly simple refactoring of <em>Foo</em>, this time we can
    inject our dependency via the setter and retrieve it via its getter. So
    which type of injection is best? Well really both are valid, using
    constructor based injection you can be certain that your dependency
    will not be forgotten to be injected as they are required to
    instantiate the object, with setter based injection you could forget to
    inject your dependency. However, with setter based injection you can
    get around this using Lazy Injection, which we will look at next.
</p>

<h5>Lazy injection</h5>
<p>
    When using setter based injection we could possibly forget to inject a
    dependency, also if we have a large amount of dependencies our code
    could look like this:
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="re1">$foo</span> <span class="sy0">=</span> <span class="kw2">new</span> Foo<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$foo</span><span class="sy0">-&gt;</span><span class="me1">setDb</span><span class="br0">&#40;</span><span class="re1">$db</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$foo</span><span class="sy0">-&gt;</span><span class="me1">setCache</span><span class="br0">&#40;</span><span class="re1">$cache</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$foo</span><span class="sy0">-&gt;</span><span class="me1">setLog</span><span class="br0">&#40;</span><span class="re1">$log</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li></ol><div class="foot">Parsed in 0.006 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    An awful lot to remember when instantiating an object, also we are lazy
    programmers so we never want to type this much!
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> setDb<span class="br0">&#40;</span>IdbConnection <span class="re1">$db</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$db</span><span class="sy0">;</span> &nbsp; &nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getDb<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">null</span> <span class="sy0">===</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="kw2">new</span> dbConnection<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db<span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="co1">// other methods..</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.016 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    Here we have simply added an if statement into the getter method for
    the database connection and if the _db property has not already been
    set we instantiate a new default <em>dbConnection</em> object. This affectively
    lazy loads our dependency for us if we have not already set one. By no
    means is this a perfect solution to our problem, what if we need to
    configure the dependent class too? Well, we then get horrible
    situations where we have configuration code with our classes or code
    that fetches the configuration or we go for more lazy configuration and
    so forth. So this is not perfect but at least it does solve some of our
    problems, this is why you will notice that I use it a lot in the
    Storefront application!
</p>

<h5>Unified Constructor</h5>

<p>
    Another important technique to note is the Unified Constuctor, this is used 
    in many ZF components already and provides a powerful way to configure an 
    object. Whilst this is not strictly Dependency Injection we should be aware
    of it as it can be used in conjunction with Lazy Injection to good affect.
</p>
<p>
    The idea of a unified Constuctor is to allow options/config to be passed into
    the constructor of the object that requires configuration, this then creates
    a construct that looks the same for many objects giving us the unified name.
</p>
<p>
    To fully explain lets look at an example.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Foo</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="re1">$_db</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re1">$options</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">setOptions</span><span class="br0">&#40;</span><span class="re1">$options</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> setOptions<span class="br0">&#40;</span><span class="re1">$options</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$options</span> instanceof Zend_Config<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$options</span> <span class="sy0">=</span> <span class="re1">$options</span><span class="sy0">-&gt;</span><span class="me1">toArray</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">elseif</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/is_array"><span class="kw3">is_array</span></a><span class="br0">&#40;</span><span class="re1">$options</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Exception<span class="br0">&#40;</span><span class="st0">'setOptions() expects either an array or a Zend_Config object'</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re1">$options</span> <span class="kw1">as</span> <span class="re1">$key</span> <span class="sy0">=&gt;</span> <span class="re1">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$method</span> <span class="sy0">=</span> <span class="st0">'set'</span> <span class="sy0">.</span> <a href="http://www.php.net/ucfirst"><span class="kw3">ucfirst</span></a><span class="br0">&#40;</span><span class="re1">$key</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re1">$this</span><span class="sy0">,</span> <span class="re1">$method</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="re1">$method</span><span class="br0">&#40;</span><span class="re1">$value</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; </div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> setDb<span class="br0">&#40;</span>IdbConnection <span class="re1">$db</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="re1">$db</span><span class="sy0">;</span> &nbsp; &nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getDb<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">null</span> <span class="sy0">===</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span>_db <span class="sy0">=</span> <span class="kw2">new</span> dbConnection<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_db<span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.066 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    So here we can see that <em>Foo</em> now accepts $options in its construct, $options
    will be either and array or Zend_Config instance that contains the objects
    configuration. The setOptions() method is called from the construct and 
    simply iterates over the $options array and calls the setters that match 
    the keys in that array. This means that along with the Lazy Injection we 
    can now override the default database connection using the unified construct. 
</p>    
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="re1">$opts</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">'db'</span> <span class="sy0">=&gt;</span> <span class="kw2">new</span> MyDbConnection<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$a</span> <span class="sy0">=</span> <span class="kw2">new</span> Foo<span class="br0">&#40;</span><span class="re1">$options</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li></ol><div class="foot">Parsed in 0.010 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    This again lets us configure our objects from outside and breaks up those
    tight dependencies, there are some obvious drawbacks to the unified 
    construct mainly that intellisense does not work and the code is
    not as clear as it could be as you need to know what the options are, though
    they do match the setters anyway.
</p>

<h4>Dependency Injection Containers</h4>
<p>
    At this point I was going to show how we can use the Yadif DI Container to 
    move all object configuration outside of our objects, however as I mentioned
    earlier after some discussion and research I now believe that the use of a
    DI container in PHP may be overkill in most situations.
</p>
<p>
    So what does a DI Container look like?
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="re1">$container</span> <span class="sy0">=</span> <span class="kw2">new</span> DIContainer<span class="br0">&#40;</span><span class="re1">$config</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$foo</span> <span class="sy0">=</span> <span class="re1">$container</span><span class="sy0">-&gt;</span><span class="me1">getObject</span><span class="br0">&#40;</span><span class="st0">'Foo'</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li></ol><div class="foot">Parsed in 0.006 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    The $config variable would contain a set a configuration instructions 
    that tells the container how various object or components should be 
    instantiated and configured. Therefore, we would retrieve all new objects
    via the container and would never use new to instantiate our objects, 
    pushing all instantiation and wiring to the container.
</p>
<p>
    So whats the problem? This does look like a very reasonable approach when
    you have many dependencies, however as PHP is a dynamic language that is 
    generally used in a web environment we have a problem with the amount of 
    objects that are created and potentially never used. Consider the following 
    diagram.  
</p>
<img src="/images/di/problem.png" />
<p>
    If we were to retrieve A from the DI Container the Container would 
    automatically instantiate six other objects, these in turn could create 
    other objects and so on. This in PHP can become impractical as generally
    we are running in a stateless environment and many of these objects may never
    be used and take up memory. In Java this approach does not matter so much as
    it is stateful and eventually the objects will be used. 
</p>
<p>
    With this in mind I would only use a DI Container if you are running PHP in
    some sort of stateful environment. For now I would suggest using a combination
    of Lazy Injection, Unified Constructor and Factories to wire you objects, 
    though a small DI Container used carefully can still be helpful. 
</p>

<h4>Conclusion</h4>

<p>
    A big part of Dependency Injection is helping us to create testable code, 
    all the manual DI techniques I have covered here help to create a testable
    codebase even without the use of a DI Container. Remember the biggest killers
    for testable code are:
</p>
<ul>
    <li>Global State</li>
    <li>new calls within your code</li>
    <li>Singletons (these are global state)</li>
    <li>Static calls (global state again...)</li>
    <li>PHP Function calls (global state yet again...)</li>
</ul>
<p>
    So to sum up, Dependency Injection techniques are very valuable in our 
    day-to-day programming, enabling us to create easy to test code. DI Containers
    probably should be used only with great care in PHP.
</p>

<p>
    <em>Footnote 1:</em> If you are interested in Yadif or want to see a DI Container in action I have 
    released code for the Storefront which I used in my experiments. <a href="http://code.google.com/p/zendframeworkstorefront/source/browse/#svn/branches/extended/dependencyInjection">Google Code - Yadif & Storefront</a>
</p>
<p>
    <em>Footnote 2:</em> Remember we can use Lazy Injection to guard against all the bad things
    for testability not just new calls. 
</p>
<p>
    <em>Footnote 3:</em> To guard against PHP core function cores which are in 
    the Global Scope we can make a proxy class.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> Php_Proxy</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __call<span class="br0">&#40;</span><span class="re1">$method</span><span class="sy0">,</span> <span class="re1">$args</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="re1">$method</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Exception<span class="br0">&#40;</span><span class="st0">'Function : '</span> <span class="sy0">.</span> <span class="re1">$method</span> <span class="sy0">.</span> <span class="st0">' not found'</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <a href="http://www.php.net/call_user_func_array"><span class="kw3">call_user_func_array</span></a><span class="br0">&#40;</span><span class="re1">$method</span><span class="sy0">,</span> <span class="re1">$args</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="co1">// We can then use the class like this</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$proxy</span> <span class="sy0">=</span> <span class="kw2">new</span> Php_Proxy<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$proxy</span><span class="sy0">-&gt;</span><span class="me1">mysql_query</span><span class="br0">&#40;</span><span class="st0">'SELECT * FROM x'</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li></ol><div class="foot">Parsed in 0.027 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
    By creating a class that proxies to the global PHP functions we can now 
    cleany replace calls to them within our classes during testing.
</p>]]></description>
      <dc:subject>PHP, Zend Framework</dc:subject>
      <dc:date>2009-12-09T21:10:01+00:00</dc:date>
    </item>

    <item>
      <title>Zend Entity Notes On Rich Domain Objects</title>
      <link>http://www.thepopeisdead.com/main/comments/zend_entity_notes_on_rich_domain_objects/</link>
      <guid>http://www.thepopeisdead.com/main/comments/zend_entity_notes_on_rich_domain_objects/#When:21:46:00Z</guid>
      <description><![CDATA[<p>
I have been testing out Zend_Entity and was wondering how to inject
dependencies into the Domain Objects it creates, after some reading I
finally came up with the following prototype. Any suggestions on a
better solution are welcome :)
<p>
<h5>The Problem</h5>
<p>
Ok so the problem is that a Domain Object has a dependency on another
Domain Object (Not one produced by Zend_Entity, those dependencies are
already handled) or Service, Zend_Entity should not know about the
Dependencies as it is not a DI Container. So how can we get these
dependencies into the Domain Object?
</p>
<h5>Why Solve it?</h5>
<p>
Well if we can not have other dependencies to our Domain Objects we are
going to end up using our Domain Objects like simple DAO's which they
are not, this will walk us into the <a href="http://en.wikipedia.org/wiki/Anemic_Domain_Model">Anemic Domain Model</a>
</p>
<h5>Example Domain Object</h5>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> My_Model_Product</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="re1">$id</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="re1">$name</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;protected <span class="re1">$_mail</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="kw2">function</span> getState<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">'id'</span> <span class="sy0">=&gt;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">id</span><span class="sy0">,</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">'name'</span> <span class="sy0">=&gt;</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">name</span><span class="sy0">,</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="kw2">function</span> setState<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a> <span class="re1">$state</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re1">$state</span> <span class="kw1">AS</span> <span class="re1">$k</span> <span class="sy0">=&gt;</span> <span class="re1">$v</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="re1">$k</span> <span class="sy0">=</span> <span class="re1">$v</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="kw2">public</span> <span class="kw2">function</span> setMail<span class="br0">&#40;</span><span class="re1">$m</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="re1">$this</span><span class="sy0">-&gt;</span>_mail <span class="sy0">=</span> <span class="re1">$m</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.027 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
We can see that our Domain Object needs to use the Mail Service, so we
need to fulfill this dependency.
</p>
<h5>A possible solution</h5>
<p>
Ok here's my prototype :)
</p>
<h5>Changes to Yadif</h5>
<p>
I am using Yadif (<a href="http://github.com/beberlei/yadif">http://github.com/beberlei/yadif</a>) for Dependency Injection, but I need to add the ability to inject into an already created instance.
</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="coMULTI">/**</span></div></li>
<li class="li1"><div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* Injects dependents into an instance</span></div></li>
<li class="li1"><div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* </span></div></li>
<li class="li1"><div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* @param mixed $instance</span></div></li>
<li class="li1"><div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* @return mixed</span></div></li>
<li class="li1"><div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;*/</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> inject<span class="br0">&#40;</span><span class="re1">$instance</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/is_object"><span class="kw3">is_object</span></a><span class="br0">&#40;</span><span class="re1">$instance</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Yadif_Exception<span class="br0">&#40;</span><span class="st0">&quot;Must be a class instance&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$name</span> <span class="sy0">=</span> <a href="http://www.php.net/strtolower"><span class="kw3">strtolower</span></a><span class="br0">&#40;</span><a href="http://www.php.net/get_class"><span class="kw3">get_class</span></a><span class="br0">&#40;</span><span class="re1">$instance</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/array_key_exists"><span class="kw3">array_key_exists</span></a><span class="br0">&#40;</span><span class="re1">$name</span><span class="sy0">,</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_container<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Yadif_Exception<span class="br0">&#40;</span><span class="st0">&quot;Component '&quot;</span><span class="sy0">.</span><span class="re1">$name</span><span class="sy0">.</span><span class="st0">&quot;' does not exist in container.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$component</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span>_container<span class="br0">&#91;</span><span class="re1">$name</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$scope</span> <span class="sy0">=</span> <span class="re1">$component</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_SCOPE</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$componentReflection</span> <span class="sy0">=</span> <span class="kw2">new</span> ReflectionClass<span class="br0">&#40;</span><span class="re1">$component</span><span class="br0">&#91;</span> self<span class="sy0">::</span><span class="me2">CONFIG_CLASS</span> <span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$constructorArguments</span> <span class="sy0">=</span> <span class="re1">$component</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_ARGUMENTS</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$setterMethods</span> &nbsp; &nbsp; &nbsp; &nbsp;<span class="sy0">=</span> <span class="re1">$component</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_METHODS</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re1">$constructorArguments</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Yadif_Exception<span class="br0">&#40;</span><span class="st0">&quot;Component '&quot;</span><span class="sy0">.</span><span class="re1">$name</span><span class="sy0">.</span><span class="st0">&quot;' has constructor injector and instance is already created.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re1">$setterMethods</span> <span class="kw1">as</span> <span class="re1">$method</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$methodName</span> <span class="sy0">=</span> <span class="re1">$method</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_METHOD</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$params</span> <span class="sy0">=</span> <span class="re1">$method</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_PARAMETERS</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$injection</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re1">$method</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_ARGUMENTS</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$argsName</span> <span class="sy0">=</span> <span class="re1">$method</span><span class="br0">&#91;</span>self<span class="sy0">::</span><span class="me2">CONFIG_ARGUMENTS</span><span class="br0">&#93;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$injection</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">injectParameters</span><span class="br0">&#40;</span><span class="re1">$argsName</span><span class="sy0">,</span> <span class="kw2">null</span><span class="sy0">,</span> <span class="re1">$params</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re1">$componentReflection</span><span class="sy0">-&gt;</span><span class="me1">getMethod</span><span class="br0">&#40;</span><span class="re1">$methodName</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">isConstructor</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw <span class="kw2">new</span> Yadif_Exception<span class="br0">&#40;</span><span class="st0">&quot;Cannot use constructor in 'methods' setter injection list. Use 'arguments' key instead.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/call_user_func_array"><span class="kw3">call_user_func_array</span></a><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re1">$instance</span><span class="sy0">,</span> <span class="re1">$methodName</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re1">$injection</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re1">$instance</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.092 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
So this is a very rough addition to Yadif and simply uses the current container implementation to inject dependents via setters, obviously construct injection is out of the question here.
</p>
<h5>Zend_Entity Event</h5>
<p>Next we need to add a new event into Zend_Entity.</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> My_Entity_Event_Test <span class="kw2">extends</span> Zend_Entity_Event_EventAbstract</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> postLoad<span class="br0">&#40;</span><span class="re1">$entity</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$f</span> <span class="sy0">=</span> Zend_Controller_Front<span class="sy0">::</span><span class="me2">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$c</span> <span class="sy0">=</span> <span class="re1">$f</span><span class="sy0">-&gt;</span><span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">'bootstrap'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getContainer</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$name</span> <span class="sy0">=</span> <a href="http://www.php.net/get_class"><span class="kw3">get_class</span></a><span class="br0">&#40;</span><span class="re1">$entity</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re1">$c</span><span class="sy0">-&gt;</span><span class="re1">$name</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$c</span><span class="sy0">-&gt;</span><span class="me1">inject</span><span class="br0">&#40;</span><span class="re1">$entity</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.033 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>This class is then registered to Zend_Entity and postLoad is called every time an entity is created.</p>
<p>
Here we get the container from the bootsrap and then use the new inject method to populate the entities dependencies.
This is a pretty lazy way to do this, it's probably better to inject the container.
</p>
<h5>Example Object Wiring</h5>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="kw2">class</span> My_Objects <span class="kw2">extends</span> Yadif_Module</div></li>
<li class="li1"><div class="de1"><span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; protected <span class="kw2">function</span> configure<span class="br0">&#40;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">&quot;My_Model_Bug&quot;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">to</span><span class="br0">&#40;</span><span class="st0">&quot;My_Model_Bug&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">&quot;My_Model_Product&quot;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">-&gt;</span><span class="me1">to</span><span class="br0">&#40;</span><span class="st0">&quot;My_Model_Product&quot;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">-&gt;</span><span class="me1">method</span><span class="br0">&#40;</span><span class="st0">'setMail'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">args</span><span class="br0">&#40;</span><span class="st0">'My_Service_Mail'</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">&quot;My_Service_Mail&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.029 seconds,  using GeSHi 1.0.7.21</div></div></div>
<p>
So here we have our object wiring where we setup the dependencies.
</p>
<h5>Example usage</h5>
<p>Finally here is an example usage:</p>
<div class="codeparser"><div class="php" style="font-family: monospace;color: #000066; border: 1px solid #d0d0d0; background-color: #f9f9f9;"><div class="head"><div class="right"><a href="#" rel="codeparser">[Download Source]</a></div><div>&copy; 2012 Keith Pope</div><div class="clear"></div></div><ol><li class="li1"><div class="de1"><span class="re1">$sqlQueryBuilder</span> <span class="sy0">=</span> <span class="kw2">new</span> Zend_Db_Mapper_SqlQueryBuilder<span class="br0">&#40;</span><span class="re1">$c</span><span class="sy0">-&gt;</span><span class="me1">Max_Entity_Manager</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1"><span class="re1">$sqlQueryBuilder</span><span class="sy0">-&gt;</span><span class="me1">fromEntity</span><span class="br0">&#40;</span><span class="st0">&quot;My_Model_Bug&quot;</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">-&gt;</span><span class="me1">where</span><span class="br0">&#40;</span><span class="st0">&quot;assigned_to = ?&quot;</span><span class="sy0">,</span> <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="nu0">1</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">-&gt;</span><span class="me1">order</span><span class="br0">&#40;</span><span class="st0">&quot;bug_created DESC&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="re1">$bugs</span> <span class="sy0">=</span> <span class="re1">$sqlQueryBuilder</span><span class="sy0">-&gt;</span><span class="me1">getResultList</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re1">$bugs</span> <span class="kw1">AS</span> <span class="re1">$bug</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; Zend_Debug<span class="sy0">::</span><span class="me2">dump</span><span class="br0">&#40;</span><span class="re1">$bug</span><span class="sy0">-&gt;</span><span class="me1">getProducts</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re1">$bug</span><span class="sy0">-&gt;</span><span class="me1">getProducts</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">as</span> <span class="re1">$product</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Zend_Debug<span class="sy0">::</span><span class="me2">dump</span><span class="br0">&#40;</span><span class="re1">$product</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li></ol><div class="foot">Parsed in 0.035 seconds,  using GeSHi 1.0.7.21</div></div></div>
<h5>Conclusion</h5>
<p>
Well this has been a very quick blog post but I hope these notes either spark debate or help someone, any suggestions are welcome.
</p>
<p>
I am still working on my Dependency Injection article, hopefully have that done soon ish and hopefully i will get some time to cover more on Zend_Entity...
</p>]]></description>
      <dc:subject>Web Development, Zend Framework</dc:subject>
      <dc:date>2009-10-28T21:46:00+00:00</dc:date>
    </item>

    <item>
      <title>Free chapter and more to come</title>
      <link>http://www.thepopeisdead.com/main/comments/free_chapter_and_more_to_come/</link>
      <guid>http://www.thepopeisdead.com/main/comments/free_chapter_and_more_to_come/#When:18:26:00Z</guid>
      <description><![CDATA[<p>
Packt have kindly sent me a chapter to post here, so here it is <a href="http://www.packtpub.com/files/4220-zend-framework-sample-chapter-7-the-shopping-cart.pdf">Chapter 7 Zend Framework 1.8 Web Application Development</a>
</p>
<p>
Now that I have more free time I will hopefully be posting some follow on articles here, these will continue on from the end of the book looking at new Zend Framework features and build upon the Storefront example application.
</p>
<p>
The first article planned on my whiteboard is Dependency Injection using the Yadif DI Container, this will hopefully be done in a month or so (I am taking it easy for a while) :)
</p>
]]></description>
      <dc:subject>Zend Framework</dc:subject>
      <dc:date>2009-09-29T18:26:00+00:00</dc:date>
    </item>

    <item>
      <title>Book released today!</title>
      <link>http://www.thepopeisdead.com/main/comments/book_released_today/</link>
      <guid>http://www.thepopeisdead.com/main/comments/book_released_today/#When:11:45:02Z</guid>
      <description><![CDATA[<p>
My book is now officially available <a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/mid/25090917885o?utm_source=thepopeisdead.com&amp;utm_medium=affiliate&amp;utm_content=blog&amp;utm_campaign=mdb_000834">zend-framework-1-8-web-application-development/</a>
<br />
</p>
<p>
Thanks to everyone who helped!
<br />
</p>
<p>
<a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/mid/25090917885o?utm_source=thepopeisdead.com&amp;utm_medium=affiliate&amp;utm_content=blog&amp;utm_campaign=mdb_000834">
<br />
<img src="http://images.packtpub.com/images/100x123/1847194222.png" />
<br />
</a>
<br />

</p>]]></description>
      <dc:subject>Personal, Web Development, Zend Framework</dc:subject>
      <dc:date>2009-09-22T11:45:02+00:00</dc:date>
    </item>

    <item>
      <title>Zend Framework Book Release Very Soon!</title>
      <link>http://www.thepopeisdead.com/main/comments/zend_framework_book_release_very_soon/</link>
      <guid>http://www.thepopeisdead.com/main/comments/zend_framework_book_release_very_soon/#When:09:47:00Z</guid>
      <description><![CDATA[<p>
We are in the final stages of getting the book finished and will hopefully be publishing by the end of the month!
</p>
<p>
I would like to thank all the people who have supported me during a very long year of writing this book, especially 
my wife who has been very patient with me, without her support none of this would have been possible, I love you very much.
</p>
<p>
It has been a very long road and I hope everyone enjoys the book!
</p>]]></description>
      <dc:subject>Personal, PHP, Web Development, Zend Framework</dc:subject>
      <dc:date>2009-09-05T09:47:00+00:00</dc:date>
    </item>

    <item>
      <title>Zend Framework Training</title>
      <link>http://www.thepopeisdead.com/main/comments/zend_framework_training/</link>
      <guid>http://www.thepopeisdead.com/main/comments/zend_framework_training/#When:17:55:00Z</guid>
      <description><![CDATA[<p>
I am thinking of providing some training courses on the Zend Framework once I have finished writing the book, I was wondering what people thought were the best subjects to cover and what sort of experience level people would like to see from a course, generally what would you like to see from a ZF course?
</p>
<p>
Thanks for any help :)
</p>]]></description>
      <dc:subject>PHP, Zend Framework</dc:subject>
      <dc:date>2009-08-17T17:55:00+00:00</dc:date>
    </item>

    <item>
      <title>Book finally announced, Zend Framework 1.8 Web Application Development</title>
      <link>http://www.thepopeisdead.com/main/comments/book_finally_announced_zend_framework_18_web_application_development/</link>
      <guid>http://www.thepopeisdead.com/main/comments/book_finally_announced_zend_framework_18_web_application_development/#When:01:24:00Z</guid>
      <description><![CDATA[<p>
My book is now officially announced <a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/mid/25090917885o?utm_source=thepopeisdead.com&utm_medium=affiliate&utm_content=blog&utm_campaign=mdb_000834">zend-framework-1-8-web-application-development/</a>
</p>
<p>
How exciting, I should have the final chapter finished this weekend, then its just cleaning everything up and we are good to go!
</p>
<p>
<img src="http://images.packtpub.com/images/100x123/1847194222.png" />
</p>]]></description>
      <dc:subject>Personal, Web Development, Zend Framework</dc:subject>
      <dc:date>2009-06-27T01:24:00+00:00</dc:date>
    </item>

    
    </channel>
</rss>
