<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Tue, 21 May 2013 02:16:09 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Juan Treminio: Unit Testing Tutorial Part V: Mock Methods and Overriding Constructors]]></title>
      <guid>http://www.phpdeveloper.org/news/19416</guid>
      <link>http://www.phpdeveloper.org/news/19416</link>
      <description><![CDATA[<p>
<i>Juan Treminio</i> has posted the <a href="http://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/">latest part of his unit testing series</a> to his site today - the fifth part that looks at using mock methods on mock objects and overriding constructors.
</p>
<blockquote>
Previously in my PHPUnit tutorial series, you learned about the very powerful concept of mock objects and stub methods. This concept is central to successful unit testing, and once it fully 'clicks' in your head you will start to realize how useful and simple testing can be. There is also another thing I want to make clear: creating tests is basically a puzzle - you simply have to go step by step, making sure all the pieces fit together correctly so you can get your green. I hope to make clear what I mean by the end of this tutorial.
</blockquote>
<p>
He assumes you already know about mock objects and introduces the concept of "stub methods" and "mock methods", noting the difference between them. He then gets into what he calls the "four pathways of getMockBuilder" and talks about the rationale behind mocking methods in the first place. He then gets into constructors and how you can work around the "bad" ones with help from mock object functionality. 
</p>
<p>
If you're interested in reading the rest of the series, you can find links to them <a href="http://jtreminio.com/tag/phpunit/">here</a>.
</p>
Link: http://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/]]></description>
      <pubDate>Fri, 05 Apr 2013 09:38:49 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Bob Majdak: Extending an Iterator to use an Iterator to make your code a little cleaner]]></title>
      <guid>http://www.phpdeveloper.org/news/19300</guid>
      <link>http://www.phpdeveloper.org/news/19300</link>
      <description><![CDATA[<p>
In <a href="http://catch404.net/2013/03/extending-an-iterator-to-use-an-iterator-to-make-your-code-a-little-cleaner/">this new post</a> to his site <i>Bob Majdak</i> talks about extending iterators to help make it easier to customize it for your needs.
</p>
<blockquote>
One of the nice things about iterators is the ability to shove them into other iterators, allowing you to wrap their functionality in other functionality to return a more precise result set. Take for example the idea that we want to read a directory to list only the images inside of it. There are two main ways to do this, via <a href="http://www.php.net/opendir>opendir</a> / <a href="http://www.php.net/readdir">readdir</a> functions and via <a href="http://www.php.net/FilesystemIterator">FilesystemIterator</a> objects. Going the FilesystemIterator route, one common way seems to be extend another class called <a href="http://www.php.net/FilterIterator">FilterIterator</a> which allows you to customize a filter based on you overwriting a method called accept().
</blockquote>
<p>
He shows not only overriding the "accept" method, but also the constructor to make using this new iterator a much simpler (and cleaner) call. You can find out more about the FilesystemIterator (and others) over in <a href="http://www.php.net/manual/en/spl.iterators.php">the Iterators section</a> of the PHP manual.
</p>]]></description>
      <pubDate>Tue, 12 Mar 2013 09:25:04 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Matt Frost: Dependency Injection Container Question]]></title>
      <guid>http://www.phpdeveloper.org/news/19192</guid>
      <link>http://www.phpdeveloper.org/news/19192</link>
      <description><![CDATA[<p>
In his <a href="http://shortwhitebaldguy.com/blog/2013/02/dependency-injection-container-question">latest post</a> <i>Matt Frost</i> takes a look at dependency injection. He thinks out loud about some of the common uses for it but wonders if there's a middle ground for using a DIC and injecting manual dependencies.
</p>
<blockquote>
The question I have is what if a dependency in one class also has a dependency? To illustrate what I mean, here's an example with some code to follow. [...] I'm not really concerned about the code here as much as I am about the concept that I'm trying to illustrate, in order to use a dependency injection container for this scenario.
</blockquote>
<p>
In his example code, he shows a "DBAuthMethod" class that extends the "AuthMethod" interface and an "Auth" class that requires an instance of "AuthMethod" as a constructor parameter. He wonders about constructor versus setter injection and thinks that a mix of the two may not be the best structure for the code. 
</p>
<blockquote>
I just can wrap my mind around a scenario where you could ONLY use a DIC, and if you can't use the concept exclusively what benefit is there to using it?
</blockquote>
<p>
Have any suggestions to share? <a href="http://shortwhitebaldguy.com/blog/2013/02/dependency-injection-container-question">Let him know</a> - this is a problem more and more developers run into as DIC becomes more widely used.
</p>]]></description>
      <pubDate>Mon, 18 Feb 2013 09:26:17 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[PHPMaster.com: Dependency Injection with Pimple]]></title>
      <guid>http://www.phpdeveloper.org/news/19103</guid>
      <link>http://www.phpdeveloper.org/news/19103</link>
      <description><![CDATA[<p>
On PHPMaster.com there's a new tutorial showing you <a href="http://phpmaster.com/dependency-injection-with-pimple/">how to use Pimple</a> (the <a href="http://pimple.sensiolabs.org/">dependency injection container</a> from the Symfony folks) in your application to manage objects and resources.
</p>
<blockquote>
In application development, we try to create independent modules so that we can reuse code in future projects. But, it's difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance nightmares unless they are managed properly. This is where Dependency Injection comes in handy, as it gives us the ability to inject the dependencies our code needs to function properly without hard coding them into the modules.
</blockquote>
<p>
They start with a look at the problem with working with "concerete dependencies", ones that are hard-coded into your classes making them not only hard to test but potentially difficult to maintain. They include an example of this (a "SocialFeeds" class and friends) and then one of two ways to fix the situation. They start with using constructor-based injection, injecting the Twitter service into the main feeds object. They also talk about another method - setter-based injection - where the objects are injected via specific methods on the object. 
</p>
<p>
As a third alternative, though, they get to using Pimple to manage the objects, making it easier to inject just the one resource into your classes and extract the objects you need from there. There's also a bit of "advanced" usage of Pimple showing the use of the "share" and "extend" methods.
</p>]]></description>
      <pubDate>Tue, 29 Jan 2013 09:37:50 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[PHPMaster.com: Constructors and the Myth of Breaking the Liskov Substitution Principle]]></title>
      <guid>http://www.phpdeveloper.org/news/18569</guid>
      <link>http://www.phpdeveloper.org/news/18569</link>
      <description><![CDATA[<p>
On PHPMaster.com there's a new post in a series looking at the <a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID</a> design principles in PHP development. In <a href="http://phpmaster.com/constructors-and-the-myth-of-breaking-the-lsp/">this new tutorial</a> they try to dispel the myth that constructors break the Liskov Substitution Principle ("L" in "SOLID").
</p>
<blockquote>
Rants aside, the morale of the story can be boiled down to the following: "Object construction is not part of the contract honored by its implementers". It's easier to grasp concepts by example rather than reading dull theory, so in this article I'll be demonstrating from a practical standpoint how the implementation of different constructors down the same hierarchy isn't a violation of the <a href="http://phpmaster.com/liskov-substitution-principle/">Liskov Substitution Principle</a>, which is a reason why you shouldn't fall into the temptation of tainting your interfaces with constructors.
</blockquote>
<p>
He illustrates the point with a simple PDO class that implements a "DatabaseAdapterInterface" interface that defines its own constructor that follows the <a href="http://us3.php.net/manual/en/pdo.construct.php">defaults</a> of the PDO extension. He goes on and changes the constructor for the class a bit to take in an array of config options rather than the DSN/User/Password combo. Inside of this constructor, those values are then taken and pushed into PDO to create the connection. He also suggests one other solution - the injection of a connection object ("ConnectionDefinition") into the constructor instead of the configuration directly.
</p>]]></description>
      <pubDate>Mon, 08 Oct 2012 11:53:13 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Ralph Schindler's Blog: PHP Constructor Best Practices And The Prototype Pattern]]></title>
      <guid>http://www.phpdeveloper.org/news/17659</guid>
      <link>http://www.phpdeveloper.org/news/17659</link>
      <description><![CDATA[<p>
In <a href="http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern">this new post</a> <i>Ralph Schindler</i> takes a look at the Prototype design pattern and uses it to illustrate some best practices in using constructors in PHP.
</p>
<blockquote>
If your knowledge of constructors ends with "the place where I put my object initialization code," read on. While this is mostly what a constructor is, the way a developer crafts their class constructor greatly impacts the initial API of a particular class/object; which ultimately affects usability and extensibility. After all, the constructor is the first impression a particular class can make.
</blockquote>
<p>
He starts at ground level, reintroducing what a constructor is and what it should (and shouldn't) be used for. He talks about constructor overloading, constructor injection, dynamic class extension and using the <a href="http://en.wikipedia.org/wiki/Prototype_pattern">Prototype pattern</a> to create "an unlimited number of objects of a particular type, with dependencies in tact, each with slight variations." He gives an example with a "DbAdapter" class, showing dynamic class instantiation and how to, using the Prototype method, inject a DbAdapter object and have your class use that instead.
</p>]]></description>
      <pubDate>Mon, 12 Mar 2012 11:26:10 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Anthony Ferrara's Blog: IteratorIterator - PHP Inconsistencies And WTFs]]></title>
      <guid>http://www.phpdeveloper.org/news/17071</guid>
      <link>http://www.phpdeveloper.org/news/17071</link>
      <description><![CDATA[<p>
<i>Anthony Ferrara</i> has a new post to his blog sharing some <a href="http://blog.ircmaxell.com/2011/10/iteratoriterator-php-inconsistencies.html">inconsistencies with iterators</a> that he discovered as discussed with a <a href="http://twitter.com/#!/go_oh">fellow developer</a> - why some iterators only accept Iterator arguments and others don't.
</p>
<blockquote>
We were talking about why some of the SPL Iterators accept only an Iterator as the constructor argument (Such as <a href="http://us2.php.net/manual/en/class.limititerator.php">LimitIterator</a>), and others accept either an Iterator or an IteratorAggregate as the argument (Such as <a href="http://us2.php.net/manual/en/class.iteratoriterator.php">IteratorIterator</a>).  Feeling that this would be a useful feature to add (having all of them accept an IteratorAggregate), I opened up the PHP source and started looking at how hard of a change this would be.  What I found was... Interesting...
</blockquote>
<p>
He shares some of the C code he came across in his investigation including a "WTF" moment when he found a <a href="http://lxr.php.net/xref/PHP_5_3/ext/spl/spl_iterators.c#1418">case statement for DIT_IteratorIterator</a> in a constructor. Because of some of the logic in this constructor, the inputted iterator is "cast down" to a class. This is shown in a few code examples comparing simple iteration objects and arrays and how it seems to be able to bypass class inheritance to use methods from other classes.
</p>]]></description>
      <pubDate>Tue, 01 Nov 2011 12:58:07 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Paul Jones' Blog: Universal Constructor Sighting "In The Wild"]]></title>
      <guid>http://www.phpdeveloper.org/news/14777</guid>
      <link>http://www.phpdeveloper.org/news/14777</link>
      <description><![CDATA[<p>
In a new post to his blog <i>Paul Jones</i> <a href="http://paul-m-jones.com/archives/1500">talks about</a> spotting something "in the wild" that he's a proponent of - a "universal constructor".
</p>
<blockquote>
For those of you who don't know, "<a href="http://solarphp.com/manual/appendix-standards.constructor">universal constructor</a>" is the name I give to PHP constructors that always and only take a single parameter. The parameter is an array of key-value pairs, which is then merged with a set of default keys and values. Finally, the array is unmarshalled, usually into object properties.
</blockquote>
<p>
He talks about the benefits of using a constructor like this in your applications and how he's implemented it as a standard part of <a href="http://solarphp.com/">the Solar framework</a>. Oh, and the sighting in the wild? It was in <a href="http://www.jqueryin.com/projects/mongo-session/">this MongoDB session handler</a>.
</p>
<p>
Be sure to <a href="http://paul-m-jones.com/archives/1500#comments">check out the comments</a> for some other great opinions on this "universal constructor" idea, both for and against.
</p>]]></description>
      <pubDate>Mon, 12 Jul 2010 09:56:22 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Patrick Allaert's Blog: Coding standards: converts PHP4 style constructors to PHP5 one]]></title>
      <guid>http://www.phpdeveloper.org/news/13456</guid>
      <link>http://www.phpdeveloper.org/news/13456</link>
      <description><![CDATA[<p>
<i>Patrick Allaert</i> has <a href="http://patrickallaert.blogspot.com/2009/10/coding-standards-converts-php4-style.html">put together a shell command</a> that can take your PHP4 code and replace its current constructors with PHP5-formatted ones.
</p>
<blockquote>
It assumes your classes are always declared with the class keyword starting at the beginning of the line and that your files have the .php extension.
</blockquote>
<p>
It uses a regular expression in a perl command to search through the current directory and look for the ".php" files to replace the "function ClassName" sort of thing with a "function __construct".
</p>]]></description>
      <pubDate>Fri, 30 Oct 2009 07:51:39 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Fabien Potencier's Blog: What is Dependency Injection?]]></title>
      <guid>http://www.phpdeveloper.org/news/12224</guid>
      <link>http://www.phpdeveloper.org/news/12224</link>
      <description><![CDATA[<p>
<i>Fabien Potencier</i> has <a href="http://fabien.potencier.org/article/11/what-is-dependency-injection">posted a look</a> at dependency injections - what they are and how they can effect your code (usually for the good).
</p>
<blockquote>
Dependency Injection is probably one of the most dead simple design pattern I know. And odds are you have probably already used Dependency Injection. But it is also one of the most difficult one to explain well. I think it is partly due to the nonsense examples used in most introductions to Dependency Injection. I have tried to come up with examples that fits the PHP world better. As PHP is a language mainly used for web development, let's take a simple Web example.
</blockquote>
<p>
His example uses a session variable, setting it to a language preference and wrapping a class around it to handle the getting and setting. The dependency injection comes in when he adds a SessionStorage class into the mix, a tool that could change the place and method where that session information is held. He suggests that the best place to set these kinds of dependencies is usually the constructor but it can be done as a setter or property injection too. It just depends on the need for the script at the time.
</p>]]></description>
      <pubDate>Fri, 27 Mar 2009 11:16:10 -0500</pubDate>
    </item>
  </channel>
</rss>
