<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ralph Schindler</title>
	<atom:link href="http://ralphschindler.com/feed" rel="self" type="application/rss+xml" />
	<link>http://ralphschindler.com</link>
	<description>Ralph Schindler</description>
	<lastBuildDate>Thu, 22 Nov 2012 15:24:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>DI, DiC, &amp; Service Locator Redux</title>
		<link>http://ralphschindler.com/2012/10/10/di-dic-service-locator-redux</link>
		<comments>http://ralphschindler.com/2012/10/10/di-dic-service-locator-redux#comments</comments>
		<pubDate>Wed, 10 Oct 2012 17:41:41 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Service Location]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=180</guid>
		<description><![CDATA[To DiC, or not to DiC: that has seemed to be the question in PHP for the last few years. Most people generally agree that injecting dependencies is the right thing to do®. For those writing a framework, or any shared codebase where extensibility or the ability to grow the codebase is a core philosophical [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>To DiC, or not to DiC: that has seemed to be the question in PHP for the last few years.  Most people generally agree that injecting dependencies is the right thing to do®.  For those writing a framework, or any shared codebase where extensibility or the ability to grow the codebase is a core philosophical tenet, not injecting dependencies is doing a disservice to the project in the long run.  So, as <a href="http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php">I&#8217;ve stated before</a>, the question becomes how do we manage the added complexity that comes with practicing dependency injection?</p>
<h2>Recapping What a DiC Is</h2>
<p>A DiC (dependency injection container) is a special type of object container that acts as one part object registry, one part object factory.  In most common and popular implementations, the container is seeded with or has access to data about what the code looks like (the class names, methods parameters), and also the runtime information (the parameters, like usernames, and application specific information.)  Sometimes there is a clear division between the two, sometimes they are blended together.  Either way, we can assert that to be a DiC, a container must do at least one of:</p>
<ul>
<li>
<p>Auto-instantiation: the ability to act as a factory, by creating objects.  This means that the container is responsible for either calling <code>new</code> or calling a method that will eventually call <code>new</code> to create an object.</p>
</li>
<li>
<p>Auto-wiring: the ability to introduce one object as a dependency to another.  This means if object A is required to successfully produce a valid object B, the container is capable of finding or producing object A before introducing it to object B. This could be through constructor injection or via setter injection.</p>
</li>
</ul>
<p>In my book, failing to achieve either of those disqualifies a container from being a DiC.</p>
<h2>What A Service Locator Is</h2>
<p>A Service Locator, on the other hand, is better defined by a usage pattern, rather than the signature (pattern) of any particular class.  This means that any object, upon being injected into a consuming object and then asked for an object, can be a service locator if it is capable of producing the requested object by a particular name or type.  Effectively, a service locator can be a special container, or not; it can be a registry, or not; or it can be a factory, but perhaps not.</p>
<p>The only stipulation is that you&#8217;ve provided an object (the service locator) as a dependency to another object so that that the consuming object can then use the provided object (the service locator) to locate any dependencies.  Instead of injecting n dependencies, you inject just one: the service locator.  In code, it looks like this:</p>
<p></p><pre class="crayon-plain-tag">$container = new ContainerThatCanFindThings(); // implements LocatorInterface
$thing = new ThingThatHasDependencies($container);</pre><p></p>
<p>The constructor for this <tt>ThingThatHasDependencies</tt>, might look like this:</p>
<p></p><pre class="crayon-plain-tag">class ThingThatHasDependencies {
    public function __construct(LocatorInterface $container) {
        $this-&gt;dependencyOne = $container-&gt;get('DependencyOne');
        $this-&gt;dependencyTwo = $container-&gt;get('DependencyTwo');
    }
}</pre><p></p>
<p>Now, you can see that any instance of <tt>ThingThatHasDependencies</tt> is now </i>Container aware</i>, which, depending on the context of the ThingThatHasDependencies, might be a good (or a bad) thing.</p>
<h2>When to Use Which, In PHP</h2>
<p>The decision of whether or not to use a Service Locator approach or use a DiC should be asked and answered not for your code base as a whole, but for each layer or component.  The Service Locator pattern should be applied only in places where the it makes the most sense.  There are a few simple questions that help you decide whether applying the Service Locator pattern to a particular component or layer within your application makes the most sense.</p>
<p>To reiterate, this is not a pattern that should be applied carte-blanche to all code.  Not all code may benefit from using a Service Locator. To help you decide, start by dissecting the structure of your code into layers and components. For example, the layers in an MVC application are Models, Controllers and Views (in that order). The components can be any supporting library code that can stand on its own.</p>
<p>Once you&#8217;ve done this, the next question to ask of each is which part could benefit from the usage of a Service Locator vs. having explicitly defined class dependencies.  To do this, there are a few considerations that help you decide:</p>
<ul>
<li>
<p>What does an instance of a particular class best represent? Is it an object with a well defined API, or does the class better serve as a collection of somewhat related methods?  (Loose API&#8217;s benefit from consuming a Service Locator.)</p>
</li>
<li>
<p>How many actual required dependencies are there? (Obj.&#8217;s with many dependencies benefit from a Service Locator)</p>
</li>
<li>
<p>Is it cumbersome to wire in a large set of dependencies? (Similar to above, put another way.)</p>
</li>
<li>
<p>How many &#8220;optional dependencies&#8221; are there? (Obj.&#8217;s with variable dependency sets benefit from a Service Locator)</p>
</li>
<li>
<p>How many required dependencies shared throughout all workflows of a given object? (Obj.’s with many dependencies benefit from a Service Locator.)</p>
</li>
<li>
<p>Is it anticipated that the number of dependencies might grow over time, or is it fixed? (Adding dependencies requires informing the consumer about how to wire new dependencies.)</p>
</li>
<li>
<p>How important is it that a particular class announce its dependencies in various method signatures? (More dependencies means more API introspection to determine how to wire an object.)</p>
</li>
<li>
<p>Is this piece of code at the center of many cross-cutting concerns? (Cross-cutting concern objects generally have lots of completely unrelated dependencies.  As such, their API, like in point number 1, means little in terms of defining the objects actual role.)</p>
</li>
</ul>
<p>In summary, if you find that if:</p>
<ul>
<li>
<p>a layer of code or component has many dependencies that are not shared by all workflows of that particular consuming object;</p>
</li>
<li>
<p>that the number of these dependencies is large and capable of growing over time;</p>
</li>
<li>
<p>that there is not much importance on objects announcing their dependencies (via constructor injection, setter or interface injection);</p>
</li>
</ul>
<p>then Service Location is by far a better solution than practicing dependency injection.</p>
<h2>An Example/Exercise In Where I Make The Distinction</h2>
<p>To better understand the considerations I&#8217;ve laid out above, I&#8217;ll demonstrate their application in an application&#8217;s architecture.</p>
<p>When dealing with an <b>MVC application architecture</b>, I prefer to practice service location for controllers, and dependency injection for models.  So, how do I go about justifying that decision?</p>
<ol>
<li>
<h4>Concerning controllers: The fact that a controller is an object and an action a method is inconsequential.</h4>
<p>Most actions could as easily do their job as a closure (like many Sintra style frameworks), or a function.  While you get some benefits by being able to tuck away helper functionality in protected methods, the greatest driving factor for controllers as objects is simply that they can group contextually similar actions together into a class.  Moreover, the public API of a controller is <i>generally</i> only ever interacted with via a dispatch framework.  Rarely do developers instantiate and call controller actions without first going through some dispatcher object.</p>
</li>
<li>
<h4>Concerning controllers: Controller dependency sets are variable per controller.</h4>
<p>For any given action within a controller, it&#8217;s dependencies will vary.  Given that you&#8217;ve grouped your User centric actions together, it goes without saying that while most might share some dependencies, it&#8217;s rare that every action will share all those dependencies.  For example, a <tt>UserController::editAction</tt> might have a dependency on a <tt>UserForm</tt> object, but <tt>UserController::showAction</tt> might not:</p>
<p></p><pre class="crayon-plain-tag">class UserController {
    public function __construct(UserForm $userForm, UserRepository $userRepository, ViewInterface $view) {
        $this-&gt;userForm = $userForm;
        $this-&gt;userRepository = $userRepository;
        $this-&gt;view = $view;
    }
    public function editAction() {
        $this-&gt;view-&gt;set('form', $this-&gt;userForm);
    }
    public function showAction() {
        $this-&gt;view-&gt;set('users', $this-&gt;userRepository-&gt;findAll());
    }
}</pre><p>
    </li>
<li>
<h4>Concerning controllers: Injecting <i>specific</i> dependencies is generally outside the scope of a dispatcher.</h4>
<p>The job of a dispatcher is not trivial.  Depending on the system, it could be parsing a HTTP route, and dissecting some information to determine what code to instantiate/invoke.  Once it&#8217;s figured out what needs to be invoked, the last thing you want is your dispatcher is to have enough contextual understanding to make decisions on what specific services need to be injected into this particular controller.  The following is a common dispatch workflow is non-complex frameworks:</p>
<p></p><pre class="crayon-plain-tag">// common variables from a router:
$routerParams = array('controller' =&gt; 'User', 'action' =&gt; 'show');

// common dispatch workflow:
$class = $routerParams['controller'] . 'Controller';
$method = $routerParams['action'] . 'Action';

// dispatch
$controller = new $class;
return $controller-&gt;{$method}($routerParams);</pre><p>
    </li>
<li>
<h4>Concerning controllers: Controllers are very much tied to the stack that dispatches them.</h4>
<p>Controllers generally already have a dependency on the framework they are being dispatched from.  Along the same lines of thinking, them understanding the type that $serviceLocator->get(&#8216;db&#8217;); does not make them any less reusable since they are already dependent on a particular framework to dispatch them.  (Unless you&#8217;re not using a framework, then this point is moot.)</p>
</li>
<li>
<h4>Concerning models: It is highly anticipated that models can be re-used outside of this particular application.</h4>
<p>That said, I find it important to be able to isolate dependencies and practice good DI in models.  After all, they are most closely related to the business problem at hand, and since they are environment agnostic, they are a prime candidate for unit testing (whereas controllers are a better candidate for some form of UI or behavior testing).  In addition, I find it important to see all dependencies up front when constructing models.  Too many dependencies generally means it&#8217;s time to go back to the drawing board.</p>
</li>
<li>
<h4>Concerning models: The API of a model is something a developer (not a framework) interacts with.</h4>
<p>That being the case, the API needs to be able to effectively communicate any dependencies to the developer, without them having to consult large amounts of documentation.</p>
</li>
</ol>
<h2>Conclusion</h2>
<p>In short, contrary to a <a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx">popular belief</a>, Service Location is not always an anti-pattern.  Patterns are not prescriptions, and service location is no exception. In some cases, using service location is beneficial, in others it is not.</p>
<div class="shr-publisher-180"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2012/10/10/di-dic-service-locator-redux/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>PHP Constructor Best Practices And The Prototype Pattern</title>
		<link>http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern</link>
		<comments>http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern#comments</comments>
		<pubDate>Fri, 09 Mar 2012 20:16:39 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=166</guid>
		<description><![CDATA[If your knowledge of constructors ends with &#8220;the place where I put my object initialization code,&#8221; 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 [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>If your knowledge of constructors ends with &#8220;the place where I put my object initialization code,&#8221; 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.</p>
<p><a href="http://php.net/manual/en/language.oop5.decon.php">Constructors</a>, in their current form, have been in PHP since 5.0.0.  Previous to 5.0, PHP loosely followed the style similar to that of C++ where the name of the method matching the name of the class would act as the class constructor.  PHP 5 brought us the <tt>__construct()</tt> &#8220;magic method&#8221; which greatly formalized the new object initialization routine.</p>
<p>Before jumping into some of the topics covered in this post, there are a few things you might want to be familiar with.  First, be familiar with the <a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)">SOLID</a> principles, particularly the S (<a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a>), the L (<a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov substitution principle</a>, commonly referred to as the LSP), and the D (<a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">dependency inversion principle</a>).  More to the point of the latter, review a previous post on <a href="http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php"></a>Dependency Injection in PHP</a> for background dependency injection specific to PHP.</p>
<h2>The Constructor Signature</h2>
<p>In PHP, you create a constructor by adding a method called <tt>__construct()</tt> to your class.  The <tt>__construct()</tt> method is an <a href="http://en.wikipedia.org/wiki/Method_(computer_programming)">instance method</a> and as such, is not marked static.  For all intents and purposes, consider the <tt>__construct()</tt> <i>magic method</i> as a special type of static object factory, one which will always return the type of the object requested via the <tt>new</tt> keyword.</p>
<p></p><pre class="crayon-plain-tag">class Foo {
    public function __construct() {
    }
}

$object = new Foo();</pre><p></p>
<p>In the above code, PHP will, upon executing <tt>new Foo()</tt>, internally create a new object from scratch, execute <tt>__construct()</tt> in the <tt>Foo</tt> class, and assign this object to the variable <tt>$object</tt>.  Pretty standard stuff.  What&#8217;s important to know here is that before <tt>new Foo()</tt>, the object did not exist.  It is this fact alone that makes this completely different from any other kind of instance method.  That said, without getting into the gritty details, it is this fact alone that excuses the <tt>__construct()</tt> method from the same rules of the LSP that might apply to other instance methods.</p>
<p>This means that all of the following are legal:</p>
<p></p><pre class="crayon-plain-tag">class Foo {
    public function __construct() {
    }
}

class Bar extends Foo {
    public function __construct(ArrayObject $arrayObj, $number = 0) {
        /* do stuff with $arrayObj and $number */
    }
}

class Baz extends Bar {
    public function __construct(Bar $bar) {
        // yes, this is the proxy pattern
    }
}</pre><p></p>
<p>The above, with <tt>E_STRICT</tt> enabled, will not produce a warning.  Yet, if you renamed all of the __construct methods to anything else, they will produce a <tt>E_STRICT</tt> warning like:</p>
<p>[code lang="bash"]<br />
Strict standards: Declaration of Bar::somemethod() should be compatible with that of Foo::somemethod()<br />
[/code]</p>
<p>Why is this the case?  Simply put, the LSP referrers to sub-types of a particular object, and since before the <tt>__construct()</tt> method, no type exists (yet).  This rules simply cannot apply to something that does not exist.  For a more detailed response, <a href="http://devzone.zend.com/1916/liskov-substitution-principle-attempted/#comment-64">go here</a>.</p>
<p>What you should take away from this is that the best-practice is that each concrete object has a constructor with a signature that best represents how a consumer should fully instantiate that particular object.  In some cases where inheritance is involved, &#8220;borrowing&#8221; the parents constructor is acceptable and useful.  Furthermore, it is encouraged that when you subclass a particular type, that your new type should, when appropriate, have its own constructor that makes the most sense to the new subtype.</p>
<p>At this point, it should be noted that most other languages do not allow constructors to be marked final, be abstract, or be marked as statics (see above on the static note).  Moreover, constructors should not appear in interfaces.  In PHP, these rules do not apply, and are all possible.  For the reasons listed above, a developer should avoid the practice of marking constructors final, making them abstract, and putting them interfaces, assuming they are trying to utilize PHP’s OO model in a SOLID way.  In PHP 5.4, it is also worth knowing that by having constructors in interfaces breaks the common expectation that subtypes are capable of creating their own constructors in favor of enforcing a particular method signature.</p>
<h2>Constructor Overloading</h2>
<p>PHP does not have method overloading.  This also applies to constructors.  A class of a specific type can only have one constructor.  Since this is the case, PHP developers sometimes loosen a methods signature in order to accommodate multiple use cases.  This is done by removing or reducing the types enforced in the constructors signature to allow for more varied types to be passed in by the consumer.</p>
<p>This is an acceptable best practice when done appropriately.  What does appropriately mean?  What is &#8220;appropriate&#8221; is, of course, very much subjective.  Generally speaking, the differences in the various signatures supported should be minimal at best,  yet meaning should still communicated through the name of the parameters.  For example, let&#8217;s take this constructor:</p>
<p></p><pre class="crayon-plain-tag">class Db {
    /**
     * @var string|array|DriverInterface $driver
     */
    public function __construct($driver) {
        if (is_string($driver)) {
            $driver = $this-&gt;createDriverFromString($driver);
        } elseif (is_array($driver)) {
            $driver = $this-&gt;createDriverFromArray($driver);
        }

        if (!$driver instanceof DriverInterface) {
            throw new Exception();
        }
    }
}</pre><p></p>
<p>The above signature <tt>__construct($driver)</tt> technically supports 3 <i>effective signatures</i>:</p>
<p></p><pre class="crayon-plain-tag">__construct(/* string */ $driver);
__construct(/* array */ $driver);
__construct(DriverInterface $driver);</pre><p></p>
<p>The actual signature has not changed, but it is represented all 3 <i>effective signatures</i> that can be further described by the PHP DocBlock.</p>
<h2>Constructor Injection</h2>
<p>At this point in the PHP community and in PHP-centric developer circles, it is generally accepted that injecting your dependencies is a best-practice.  How developers go about injecting these dependencies is still very much debated and, in-part, up to personal and/or team preference.</p>
<p>There are several such methods of dependency injection: interface injection, setter injection and constructor injection to name the primary forms.  For the purposes of this post, constructor injection is our primary candidate for discussion.</p>
<p>In short, constructor injection is a pattern of injecting all of your required dependencies into a constructor.  These dependencies are usually other objects, often called <i>services</i>.  The primary benefit of constructor injection is that after you instantiate the target object, generally, it is in the complete “ready state,” meaning that it is ready to do real work.  A typical constructor signature sporting constructor injection looks like this:</p>
<p></p><pre class="crayon-plain-tag">class UserMapper {
}

class UserRepository {
    public function __construct(UserMapper $userMapper) {
        $this-&gt;userMapper = $userMapper;
    }
}</pre><p></p>
<p>The above example clearly demonstrates that before a developer can use a <tt>UserRepository</tt> object, they must first inject it with a <tt>UserMapper</tt> object.</p>
<p>In PHP, while in recent times we&#8217;ve started favoring dependency injection (which can add some complexity to code), we have traditionally gravitated towards code that is easy write and easy to use.  Practicing good dependency injection can be tedious at times and, in many cases, dependencies for objects can be stubbed by a sensible default.  This practice is also known by the name of <a href="http://en.wikipedia.org/wiki/Poka-yoke">Poka-Yoke</a>.  It allows us to develop an API that supports explicit injection of dependencies while promoting ease of use in common or majority use cases.  Consider the following code:</p>
<p></p><pre class="crayon-plain-tag">class UserMapper implements UserMapperInterface {
}

class UserRepository {
    protected $userMapper;
    public function __construct(UserMapperInterface $userMapper = null) {
        $this-&gt;userMapper = ($userMapper) ?: new UserMapper;
    }
}</pre><p></p>
<p>While the <tt>UserRepository</tt> allows you to inject your dependency of the <tt>UserMapper</tt>, it will, if one was not provided, instantiate a sensible default <tt>UserMapper</tt> for you.  The benefits are that in the most common use cases, it is a one step usage scenario (just instantiate the <tt>UserRepository</tt>). But in unit testing scenarios or scenarios where you want to inject an <i>alternate implementation</i> of a <tt>UserMapper</tt>, that can be achieved through the constructor.</p>
<h2>Dynamic Class Instantiation</h2>
<p>Generally speaking, the following code, while legal, should be used very seldom, and only when other possible instantiation patterns have been exhausted:</p>
<p></p><pre class="crayon-plain-tag">$obj = new $className();
if (!$obj instanceof SomeBaseType) {
    throw new \InvalidTypeException();
}</pre><p></p>
<p>Why is this a bad pattern?  First, it makes the assumption up front that the constructor signature is free from any required parameters.  While this is good for object types that are already known to this factory, it might not always be true of a consumers subtype of the base object in question.  This patten should never be used on objects that have dependencies, or in situations where it is conceivable that a subtype might have dependencies because this takes away the possibility for a subtype to practice constructor injection.</p>
<p>Another problem is that instead of managing an object, or a list of objects, you are now managing a class name, or list of class names in addition to an object or list of objects.  Instead, one could simply manage the objects.</p>
<p>If, on the other hand, you know this particular object type is no more than a <a href="http://c2.com/cgi/wiki?ValueObject">value object</a> (or similar), with no chance of it needing dependencies in subtypes, you can then cautiously use this instantiation pattern.</p>
<h2>Prototype Pattern</h2>
<p>So how does one create an unlimited number of objects of a particular type, with dependencies in tact, each with slight variations? Enter the <a href="http://en.wikipedia.org/wiki/Prototype_pattern">prototype pattern</a>.  This is an important pattern to keep handy when you know that you&#8217;ll have objects that need to be replicated in some way and they also have service dependencies that need to be injected.</p>
<p>To draw a parallel, this is similar to how Javascript handles its <a href="http://en.wikipedia.org/wiki/JavaScript#Prototype-based">object model</a>.  To sum prototyping up in Javascript: functions and properties are defined once per prototype rather than once per object.  The <tt>new</tt> keyword instructs the engine/runtime to create a copy of the prototype and assign to a variable for further specification and interaction.</p>
<p>This is similar to what the Prototype Pattern does in an object-oriented inheritance model.  Up front, you create a prototypical instance.  This instance will have all its dependencies injected, and any shared configuration and/or values setup.  Then, instead of calling <tt>new</tt> again, a factory (or the consumer) will call <tt>clone</tt> on the object (a shallow clone will be made), and a new object will be created from the original prototypical object.  This newly cloned object can then be further specified, injected with the variations that make this new object unique, thus interacted with as a unique object.</p>
<p>Lets consider the following example involving a database connection and the <a href="http://martinfowler.com/eaaCatalog/rowDataGateway.html">Row Gateway pattern</a>. We want to iterate a dataset from a database and during iteration, present each row as a <tt>RowGateway</tt> object.  One way of handling this would be to get the array of data from the database, then during iteration, create a new <tt>RowObject</tt> from scratch injecting the database connection:</p>
<p></p><pre class="crayon-plain-tag">class DbAdapter {

    public function fetchAllFromTable($table) {
        return $arrayOfData;
    }

}

class RowGateway {

    public function __construct(DbAdapter $dbAdapter, $tableName, $data) {
        $this-&gt;dbAdapter = $dbAdapter;
        $this-&gt;tableName = $tableName;
        $this-&gt;data = $data;
    }

    /**
     * Both methods require access to the database adapter
     * to fulfill their duties
     */
    public function save() {}
    public function delete() {}
    public function refresh() {}
}

class UserRepository {

    public function __construct(DbAdapter $dbAdapter) {}

    public function getUsers() {
        $rows = array();
        foreach ($this-&gt;dbAdapter-&gt;fetchAllFromTable('user') as $rowData) {
            $rows[] = new RowGateway($dbAdapter, 'user', $rowData);
        }
        return $rows;
    }
}</pre><p></p>
<p>A <tt>UserRepository</tt> will be constructed with a database adapter object.  It will then query the database, returning an array of all the rows that satisfied that query.  With each row of data, it will create a fresh <tt>RowObject</tt> from scratch, injecting all the dependencies, configuration and the row data.</p>
<p>At first glance, you might ask &#8220;what if I have a specialized version of <tt>RowGateway</tt> I want to use?&#8221;  That solution can be easily handled by instead of hard-coding the <tt>RowGateway</tt> class, but by use the <i>Dynamic Class Instantiation</i> pattern described above:</p>
<p></p><pre class="crayon-plain-tag">class UserRepository {

    public function __construct(DbAdapter $dbAdapter, $rowClass = 'RowGateway') {}

    public function getUsers() {
        $rows = array();
        foreach ($this-&gt;dbAdapter-&gt;fetchAllFromTable('user') as $rowData) {
            $rowClass = $this-&gt;rowClass;
            $row = new $rowClass($dbAdapter, 'user', $rowData);
            if (!$instance of RowGateway) {
                throw new InvalidClassType();
            }
            $rows[] = $row;
        }
        return $rows;
    }
}</pre><p></p>
<p>This partially solves the problem in that now we can now use our own specialized class for the <tt>RowGateway</tt> implementation, but this too has its own special set of limitations.  First, we are incorrectly making the assumption that the constructor signature of a subtype of <tt>RowGateway</tt> is exactly the same as the base type.  This means that if a subtype has additional dependencies, that class will need to do the <a href="http://www.google.com/search?q=statics+evil">static dance</a> in order to locate and consume those dependencies that it needs to achieve its specialized functionality.  By making this assumption of the classes constructor signature, we&#8217;re limiting the consumers ability to practice polymorphism in the subtypes that they might need to have created.</p>
<p>For example, if a consumer wanted to be able to have a <tt>RowGateway</tt> object that wrote data to one specific database, but refreshed its data from a different database, how might one be able to inject two different <tt>DbAdapters</tt> into a <tt>RowGateway</tt> object to achieve this end result?</p>
<p>The answer is to use the <em>Prototype Pattern</em>, and in practice (via pseudo-code), looks like this:</p>
<p></p><pre class="crayon-plain-tag">class DbAdapter {
    // same as before
}

class RowGateway {

    public function __construct(DbAdapter $dbAdapter, $tableName) {
        $this-&gt;dbAdapter = $dbAdapter;
        $this-&gt;tableName = $tableName;
    }

    public function initialize($data) {
        $this-&gt;data = $data;
    }

    /**
     * Both methods require access to the database adapter
     * to fulfill their duties
     */
    public function save() {}
    public function delete() {}
    public function refresh() {}

}

class UserRepository {

    public function __construct(DbAdapter $dbAdapter, RowGateway $rowGatewayPrototype = null) {
        $this-&gt;dbAdapter = $dbAdapter;
        $this-&gt;rowGatewayPrototype = ($rowGatewayPrototype) ? new RowGateway($this-&gt;dbAdapter, 'user')
    }

    public function getUsers() {
        $rows = array();
        foreach ($this-&gt;dbAdapter-&gt;fetchAllFromTable('user') as $rowData) {
            $rows[] = $row = clone $this-&gt;rowGatewayPrototype;
            $row-&gt;initialize($rowData);
        }
        return $rows;
    }

}</pre><p></p>
<p>By using a prototypical instance as the base for all future instances, we now allow the consumer the ability to extend this base implementation using sound object-oriented/polymorphic best-practices to achieve their end result.  So, assuming our above example of the read/write adapter, a consumer can write:</p>
<p></p><pre class="crayon-plain-tag">class ReadWriteRowGateway extends RowGateway {
    public function __construct(DbAdapter $readDbAdapter, DbAdapter $writeDbAdapter, $tableName) {
        $this-&gt;readDbAdapter = $readDbAdapter;
        parent::__construct($writeDbAdapter, $tableName);
    }

    public function refresh() {
        // utilize $this-&gt;readDbAdapter instead of $this-&gt;dbAdapter in RowGateway base implementation
    }
}


// usage:
$userRepository = new UserRepository(
    $dbAdapter,
    new ReadWriteRowGateway($readDbAdapter, $writeDbAdapter, 'user')
);
$users = $userRepository-&gt;getUsers();
$user = $users[0]; // instance of ReadWriteRowGateway with a specific row of data from the db</pre><p></p>
<h2>Parting Words</h2>
<p>Be nice to people who want to consume and extend your code.  A constructor is more than just a place for initialization code.  How you craft your constructors, the patterns you use for their signatures, and how you expect to get new instances of objects greatly affects the ability of consumers to extend your code without having to jump through too many hoops in order form them to achieve their specialized use case.  It is always better to fall back on SOLID object-oriented practices than to limit someones possibilities by forcing them into coding patterns that require reading in-depth documentation on how the original author expects someone to extend their code.</p>
<div class="shr-publisher-166"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Autoloading (Revisited)</title>
		<link>http://ralphschindler.com/2011/09/19/autoloading-revisited</link>
		<comments>http://ralphschindler.com/2011/09/19/autoloading-revisited#comments</comments>
		<pubDate>Mon, 19 Sep 2011 20:43:16 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=154</guid>
		<description><![CDATA[Upon the arrival of PHP 5.0, the ability to autoload classes was introduced. At the time, autoloading was such a new feature, it was hardly adopted. As such, many applications being ported from PHP4 to PHP5 still had lots of procedural code in them (code incapable of being be autoloaded) and many class files which [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Upon the arrival of PHP 5.0, the ability to autoload classes was introduced. At the time, autoloading was such a new feature, it was hardly adopted. As such, many applications being ported from PHP4 to PHP5 still had lots of procedural code in them (code incapable of being be autoloaded) and many class files which had long &#8216;<tt>require_once</tt>&#8216; lists. It wasn&#8217;t until years later that certain best practices had emerged and the prolific usage of <tt>require_once/include_once</tt> throughout large bodies of code had started drying up. Even after autoloading had been adopted by larger more visible projects, a common patten had yet to emerge.  The PEAR project had already had its one-class-per-file rule, and a class to filesystem naming convention, but this was hardly the rule at the time, and as such, there were many different patterns of autoloading strategies.</p>
<p>As time has passed, slowly, more and more projects had gone through re-writes and the strategy that most projects were landing on was the one that came from the PEAR group. Fast-forward to today, and we see that this standard for autoloading has agreed upon by a large number of projects and has come to be named the &#8220;PSR-0 autoloading standard&#8221;.</p>
<h2>What We&#8217;ve Learned</h2>
<p>After having attained a consistency (for code) in how we utilize autoloading, we&#8217;ve attempted to find the most efficient and performance optimized way of executing our autoloading strategy. Matthew Weier O&#8217;Phinney <a href="http://weierophinney.net/matthew/archives/245-Autoloading-Benchmarks.html">has blogged</a> about this in the past, it&#8217;s a good read if you have not already read it. To summarize, he found the following things to be true:</p>
<ul>
<li>disk based class name to filesystem location maps are the fastest lookups</li>
<li>class filesystem paths that are absolute and that do not rely on include_path are fastest</li>
<li>lightweight autoload functions that utilize class maps directly are the fastest</li>
</ul>
<p>For more information about the above generalization, see Matthew&#8217;s blog post.</p>
<p>Nearly a year ago, in conjunction with his findings, Matthew also wrote a classmap generation <a href="https://github.com/weierophinney/zf2/blob/master/bin/classmap_generator.php">tool</a>. This tool produced a <tt>.classmap.php</tt> file that would reside in the directory responsible for containing class files. The general idea here is that a developer could utilize a automatic mapping based autoloader, like the PSR-0 autoloader, or, he could utilize this <tt>.classmap.php</tt> file in order to build a more performance centric strategy for his/her autoloading needs.</p>
<p>This approach presents developers with two primary problems. One, dot files are generally hidden on a filesystem, and as such, this means that this PHP data array is also part of a code-path that is hidden from most developers view of the codebase. This then lead to moments of confusion when something related to the location of classes goes awry. The second of the problems it that this strategy assumes that the consumer has some way of consuming the contents of this class-map file. For ZF users, they could utilize one of the shipped <tt>Zend\Loader</tt> classes that are designed to use a class-map. The problem here is not necessarily for ZF users, but that it is promoting a strategy that is more ZF specific than generic in nature.</p>
<p>The addition of, and swift adoption of PHP&#8217;s namespace support in PHP 5.3 has also presented us with both a platform for standardization as well as a few challenges. Traditionally, when we thought of the PEAR naming convention, we assumed that for a given class (in prefix notation) <tt>Alpha_Beta_Gamma</tt>, there would be a single mapping of this class to a single place on the filesystem, namely: <tt>some/path/Alpha/Beta/Gamma.php</tt>. This inherently presents no problems. What does present a problem is if we have another project that utilizes part of this prefix, but in a different location. Assume that you want to use part of the prefix, for example, the Alpha_Beta_ portion, with a different logical component/module/project within your organization. In this case, it might make sense that class <tt>Alpha_Beta_Gamma</tt> live in one project on disk, and that <tt>Alpha_Beta_Omega</tt> live somewhere completely different. Any number of situations could realistically present this problem, but the most apparent is that your organization wants to utilize a naming scheme that allows for <tt>MyCompany_MyDivisionWithinMyCompany_PerhapsSomeLogicalComponent_ClassName</tt>.</p>
<p>In any of the likely scenarios of the above, a simple mapping rule that might govern one class name to filesystem name autoloader will not work for another class that could conceivably within the same project without some kind of either autoloader filter, or filesystem munging. Either way, we can no longer make the assumption that a simple map of class name to one location on disk mapping will suffice.</p>
<p>More an more, we are seeing this pattern emerge, (this time with namespace):</p>
<p></p><pre class="crayon-plain-tag">namespace VendorName\ComponentName {

    class SomeComponentClass {

    }

}</pre><p></p>
<p>This class is then found inside its own logical project, with its own data files, web files, or test files in a project structure that looks similar to this:</p>
<p>[code lang="bash"]<br />
path/to/VendorName_Component/<br />
    src/<br />
        VendorName/<br />
            ComponentName/<br />
                SomeComponentClass.php<br />
    data/<br />
        some-data-file.txt<br />
    tests/<br />
        phpunit.xml<br />
        phpunit-bootstrap.php<br />
        VendorName/<br />
            ComponentName/<br />
                SomeComponentClassTest.php'<br />
    docs/<br />
        some-documentation-format.xml<br />
    README.md<br />
[/code]</p>
<p>As you can imagine, any one vendor/organization who&#8217;s in the business of building software will more than likely have more than one project that both utilizes this kind of naming scheme and also takes advantage of this listed project structure for developing and releasing this bit of code. This being the case, unless the project is merged with other code for the purposes of a consuming project, parts of the namespace will exist in two separate parts of the filesystem &#8230; something which, a specialized autoloader will need to take into consideration.</p>
<p>Ideally, we should find a solution that will present class-map based autoloading in a way that is an easily identifiable code pattern, simple, expressive, works well with common development practices and takes advantages of the current day PHP platform (namespaces and autoloading facilities).</p>
<h2>And, What I&#8217;ve Found Is This &#8230;</h2>
<p>And, what I&#8217;ve found is that projects should present a few different options as per how they provide an &#8220;out-of-the-box&#8221; experience as it relates to autoloading. Such a solution should offer the consumer a usage story that consists of the most minimal of requirements when it comes to bootstrapping this 3rd party code. Let&#8217;s examine the following project structure (expanded from our example above):</p>
<p>[code lang="bash"]<br />
path/to/VendorName_Component/<br />
    src/<br />
        VendorName/<br />
            ComponentName/<br />
                SomeComponentClass.php<br />
    data/<br />
        some-data-file.txt<br />
    tests/<br />
        phpunit.xml<br />
        phpunit-bootstrap.php<br />
        VendorName/<br />
            ComponentName/<br />
                SomeComponentClassTest.php'<br />
    docs/<br />
        some-documentation-format.xml<br />
    autoload_classmap.php<br />
    autoload_function.php<br />
    autoload_register.php<br />
    README.md<br />
[/code]</p>
<p>What you&#8217;ll notice is the addition of 3 <tt>autoload_*.php</tt> files. Let&#8217;s have a look at what these files provide and the reasons for their existence. First the <tt>autoload_classmap.php:</tt></p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
return array(
    'VendorName\Component\SomeComponentClass' =&amp;gt; __DIR__ . '/src/VendorName/ComponentName/SomeComponentClass.php'
    /* .. other classes here .. */
);</pre><p></p>
<p>This file provides the exact map of the classname to the location on disk that this class can be found in. This file takes advantage of PHP&#8217;s ability to have return values returned from the inclusion of a file. A simple usage story for this file might be:</p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
// ...
$classmapAutoloader = new MyClassMapAutoloader();
$classmapAutoloader-&amp;gt;loadClassMap(include __DIR__ . '/vendors/VendorName_Component-1.5/autoload_classmap.php');
// ...</pre><p></p>
<p>Let&#8217;s next look at the <tt>autoload_function.php</tt> file:</p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
return function ($class) {
    static $classmap = null;
    if ($classmap === null) {
        $classmap = include __DIR__ . '/autoload_classmap.php';
    }
    if (!isset($classmap[$class])) {
        return false;
    }
    return include_once $classmap[$class];
};</pre><p></p>
<p>This file provides a closure based autoloader as its return value. This function can then be used by the consumer directly for injecting into their own autoloader stack/queue, or directly into the autoloader queue provided by PHP:</p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
// ...
spl_autoload_register(include __DIR__ . '/vendors/VendorName_Component-1.5/autoload_function.php');
// ... or ...
$autoloader = new MyFancyAutoloader();
$autoloader&amp;gt;registerAutoloaderFunction(include __DIR__ . '/vendors/VendorName_Component-1.5/autoload_classmap.php');</pre><p></p>
<p>Either way, the consumer is provided with a callback that is capable of being utilized, in a single line, to bootstrap this components autoloading needs.</p>
<p>Finally, the complete, one line solution can be found by utilizing <tt>autoload_regsiter.php</tt> directly:</p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
// autoload_register.php
spl_autoload_register(include __DIR__ . '/autoload_function.php');</pre><p></p>
<p>While the above is so trivial as to ask why it should be included, it does offer a single-line usage story:</p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php
// ...
require_once __DIR__ . '/vendors/VendorName_Component-1.5/autoload_register.php';</pre><p></p>
<p>Why not do this in the first place? Well, this approach is assuming the consumer does not necessarily care about how the autoload function is loaded into PHP&#8217;s <tt>spl_autoload</tt> queue. One thing to keep in mind is that when <tt>spl_autoload_register()</tt> is called, autoloaders are placed as the end of the queue by default. This behavior can be changed by passing true as the 3rd parameter of <tt>spl_autoload_register()</tt>. This type of performance optimization might be important when you know some autoload-able code will be utilized more often than other code, and thus you want the autoloader for that code to be consulted first. Another reason for this kind of user registration is that some autoloaders might be so generic as to want to act as a fallback autoloader or a generic autoloader. For these kind of autoloaders, it is important that they always be last in the queue since they might throw an error or exception when they cannot find a class as opposed to returning false and letting other autoloader have an attempt at finding the class requested.</p>
<h2>Conclusion</h2>
<p>The above mentioned strategy is something to be considered if you are creating reusable PHP components that you wish provide perhaps as Pyrus packages and/or as PHP phar archives for 3rd party consumption. This autoloading strategy provides an out-of-the-box usability experience in minimal amount of code. It also plays nice with other autoloaders, provides a solution that is opcode cacheable, and since it utilizes absolute paths (via <tt>__DIR__</tt>) &#8211; minimizes the amount of stat() calls to the filesystem your application will generate during its runtime.</p>
<div class="shr-publisher-154"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2011/09/19/autoloading-revisited/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Learning About Dependency Injection and PHP</title>
		<link>http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php</link>
		<comments>http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php#comments</comments>
		<pubDate>Wed, 18 May 2011 20:25:09 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=144</guid>
		<description><![CDATA[Over the past few years, there are a few concepts and programming patterns that have muscled their way into the hearts and minds of PHP developers from other languages and programming communities. These concepts range from the MVC application architecture as well as various modeling techniques (think ActiveRecord and Data Mapper), to a pure shift [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Over the past few years, there are a few concepts and programming patterns that have muscled their way into the hearts and minds of PHP developers from other languages and programming communities.  These concepts range from the MVC application architecture as well as various modeling techniques (think ActiveRecord and Data Mapper), to a pure shift in the way we think about application architectures, like aspect-oriented programming (AoP) and event-driven programming.  Perhaps it&#8217;s because PHP has been adopted at an enterprise level thus increasing the demand for what developers might call <i>enterprise quality programming patterns</i>, or perhaps it&#8217;s simply because of PHP&#8217;s ever evolving object model that makes new things possible.  After all, who doesn&#8217;t like new shiny things?  Whatever the reason, one of the newest concepts (at least over the past 3 years or so) that has emerged as one of our heated topics of debate is <em>how to manage object dependencies</em>.  Interestingly, the argument of how to manage dependencies is generally named by the solution which its proponents give as <strong>the solution</strong>: <em><a href="http://en.wikipedia.org/wiki/Dependency_injection">dependency injection</a></em> (the abstract principle is actually called <a href="http://en.wikipedia.org/wiki/Inversion_of_control">Inversion of control</a>).</p>
<p>In any circle of developers that are of the object-oriented persuasion, you&#8217;ll never hear an argument that dependency injection itself, is bad.  In these circles, it is generally accepted that injecting dependencies is the best way to go.  Injecting object dependencies in PHP looks like this:</p>
<p></p><pre class="crayon-plain-tag">// construction injection
$dependency = new MyRequiredDependency;
$consumer = new ThingThatRequiresMyDependency($dependency);</pre><p></p>
<p>That&#8217;s basically it.  There are many variations of this: setter injection, interface injection, call time injection, in addition to the above mentioned constructor injection. These are all valid ways of injecting the dependencies into the consuming object.  Ultimately, the goal here is to avoid this:</p>
<p></p><pre class="crayon-plain-tag">class ThingThatHasAnExternalDependency
{
    public function __construct() {
        $this-&amp;gt;dependency = new ARequiredDependency;
        // or 
        $this-&amp;gt;secondDependency = ARequiredDependency::getInstance();
    }
}</pre><p></p>
<p>The above code is an example of a violation of the <a href="http://en.wikipedia.org/wiki/Hollywood_Principle">Hollywood Principle</a>, which basically states: &#8220;Don&#8217;t call us, we&#8217;ll call you.&#8221;.</p>
<p>Yet, this is not the heart of the argument.  Perhaps it was 4-5 years ago in the PHP community, but it&#8217;s not anymore.  The heart of the argument is not should we be doing it, but <em>how do we go about doing it</em>.  </p>
<p>This article is not about the intricacies and implementation details of DI containers and DI frameworks.  It&#8217;s also not about the various ways and means of injecting dependencies into other objects, or which method might be better.  In fact, this article has no opinion if injecting dependencies is even good for you or your application.  This article is an exploration how adopting any DI framework for PHP affects the lifecycle of a project, both the code as well as the developer, team or organization that is constructing it.</p>
<h3>A Brief History of Dependency Management In PHP</h3>
<p>It is important to know why PHP is as popular as it is, after all, it&#8217;s this popularity that DI Frameworks fight against for adoption inside a PHP application framework.  To understand PHP&#8217;s popularity, history, and evolution, let&#8217;s look at this code:</p>
<p></p><pre class="crayon-plain-tag">// these 6 lines actually represent 5 different web centric &amp;quot;langauges&amp;quot;!
include_once 'includes/config.php'; // ultimately there is a mysql_connect() call in here somewhere
include_once 'templates/header.php';
$rows = mysql_query('SELECT * FROM users'); // magically uses the mysql_connect() resource
foreach ($rows as $row) {
    echo '&amp;lt;div class=&amp;quot;user-row&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;/delete-user.php&amp;quot; onclick=&amp;quot;someJSFunction();&amp;quot;&amp;gt;' . $row['username'] . '&amp;lt;/div&amp;gt;';
}
include_once 'templates/footer.php';</pre><p></p>
<p>From the beginning, we&#8217;ve been trained into thinking that our dependencies are magically managed.  As you can see above, the mysql_query() function, while it will accept a connection resource, does not require it.  In fact, if it&#8217;s not supplied, it will use the first open mysql connection it can find inside the PHP runtime.  Assuming that the above mentioned delete-user.php script is part of a larger collection of PHP scripts, which we will call &#8220;the application&#8221; &#8230; it is important to note that even this script itself is pulling in its dependencies instead of them being injected.  For all intents and purposes, the config.php, header.php and footer.php are all dependencies of this script, much like other scripts similar in nature to this delete-user.php.  To sum it up, if there is a new dependency that is now required by the business logic portion of this application (ie: the lines between the header and footer), they now have to be introduced to <em>all scripts</em> in this application.  This does not exactly adhere to the <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a> principle.</p>
<p>But, let&#8217;s take a step back and look at this snippet of code from the organizational perspective.  To do this, we must first understand the various phases of the code&#8217;s lifecycle within any organization.  For the purposes of this example, let&#8217;s assume that from idea to production, code will go through the following phases: development, build, deployment, to application start-up (in production).  If this were a C/C++ or Java project, code will have been written (developed), it will have been compiled (built), then it would have been packaged or some deployment tool&#8217;s process invoked (deployed); it them would have been run (executed via some startup script, or executing a binary.)  PHP, and Perl at the time, achieved all of the same objectives but in fewer steps making it a wildly popular platform for highly iterative web projects.  This same application in PHP would have been coded in some text editor (developed), and FTP&#8217;d up to a production server (deployed).  You&#8217;ll notice that it neither had to be built/compiled, or started on the server since the target, Apache, was already running with PHP embedded into it.  For all intents and purposes, a cheap and easy FTP tool was both the build and deployment tool for this application&#8217;s lifecycle.</p>
<p>It was this simplicity that made PHP the popular choice for web applications.  This popularity was attained because the simplicity of the PHP platform allowed for two extremely important facets of development to emerge: the idea of building an application became approachable to even the novice individual, and without all the cruft that came along with the application lifecycle, building and deploying applications in PHP increased PHP&#8217;s &#8220;fun-ness&#8221; factor.</p>
<p>While this style of building applications allowed for a proliferation of PHP applications to be developed, there was in fact a negative side to be revealed later in time.  As applications quickly grew, their ability to be maintained decreased.  We give them the name <a href="http://en.wikipedia.org/wiki/Spaghetti_code">&#8220;Spaghetti code&#8221;</a>, and for all the right reasons.  Objects, if they were even being used, were generally wrappers around procedural functionality.  So object dependency management wasn&#8217;t even a consideration for most developers.  Looking back, perhaps it was this original simplicity that allowed developers to create applications without even having to know what a dependency was or how to find it.  In any case, as these applications grew uncontrollably, maintaining them and hacking them started to lose the PHP fun factor exponentially.</p>
<h3>A Brief History of DI Frameworks</h3>
<p>As PHP developers started identifying the problems with their <a href="http://en.wikipedia.org/wiki/Model_1">Model 1</a> applications, they started looking for solutions in other programming communities.  At this time, the Java community was still heavily rooted in the enterprise/software development/software engineering world, and problems such as dependency management already had some interesting solutions.  Most notably, there was the <a href="http://en.wikipedia.org/wiki/Spring_Framework">Spring Framework</a>, who&#8217;s primary facility for dependency management was a component called the IoC Container, or the Inversion of Control container.  This container managed the fully lifecycle of object creation using callbacks.  This meant that you no longer has to use the &#8220;new&#8221; keyword (the same new keyword in PHP).  Also, it wired the dependencies for you at instantiation time.  This meant that you no longer had to concern yourself with how dependencies were injection; be it through the constructor, properties or setter methods.  The Spring Framework was one of the first frameworks that encouraged the use of definition files to manage the knowledge required to wire all your dependencies together.  True to form in the Java community, these definition files were created in XML.</p>
<p>As it might seem, this is indeed a deviation from the PHP philosophy that had made PHP so popular.  PHP allowed you to write the most minimal amount of code to complete your application.  In the Java/DI world, particularly with the Spring framework, you had a much richer application lifecycle.  Not only were you developing code for your appliation, but you were creating code about code to manage code.  This is known as <a href="http://en.wikipedia.org/wiki/Meta-programming">meta-programming</a>.  In addition to this meta-programming that was going on, you also now had this compilation phase required by the Java platform which was generally tucked away inside your build time tasks.  Moreover, this application had to be deployed (there were generally tools for this too), and (for good measure), due to the platform, your application had to be started.  Needless to say, this application lifecycle might seem <em>heavier</em>, for lack of a better term, to the average PHP developer.</p>
<p>Since then, several frameworks have cropped up that sport some kind of dependency management.  Before this technique was picked up in PHP, they were all heavily rooted in the Java and .NET communities.  A quick google search will return a few notable names like PicoContainer, Spring.NET, Unity, Butterfly and google-guice to name a few.  These frameworks attain popularity since they attempt to ease some of the burdens that DI places upon the developer whether it be by using reflection to create definitions, or even adding an annotation system so that DI definitions can be written inside the code they are set to manage.</p>
<h3>DI and PHP</h3>
<p>To understand the attainability of having a dependency management framework for PHP, one should first understand how the counterparts in Java and .NET rely upon their respective platforms to do certain jobs.  For a quick reference, see the images from this <a href="http://ralphschindler.com/2010/05/06/phpundamentals-series-a-background-on-statics-part-1-on-statics">blog post</a>.  One of the more important facets to remember is that the expected application lifecycle of a Java/.NET application is much richer.  You are expected to have build-time tasks.  You are expected to have deployment tasks.  And, generally, your application understand the difference between being in development, staging and production &#8211; so it can adjust how it runs accordingly.  Moreover, the platform itself has facilities in place that aid the developer both in development time with code generation as well as in production.</p>
<p>PHP never expects or facilitates the usage of any kind of build-time tasks.  PHP also does not have any kind of built-in annotation support (a meta-programming technique), nor does it have any kind of application scope or per-application memory space.  What does this mean for someone who is creating a DI container? Let&#8217;s explore.</p>
<h4>Development Time</h4>
<p>General speaking, any time you are writing, altering or just shifting code around, you are in development mode, your application should be running in a development environment.  The structure of your application&#8217;s classes, functions and files within the filesystem is probably changing with each time you click save.  Dependency management systems require knowledge of your code in order to effectively do their job.  This knowledge generally comes in the form of some kind of definition.</p>
<p>This definition can be created by hand, by the developer, generated at runtime by some application hooks, or generated with the use of a special tool.  If this is done by hand, a developer is required to explicitly map the various functions/methods that will need to be called in order to inject a particular object dependency.  The more dependencies you have, the more verbose this definition might become.</p>
<p>A better route would be to generate this definition file, after all, the code you&#8217;ve written, if written correctly will self-describe its dependencies.  There are two options for generation, manual and automatic.  An example of manual generation would be a developer giving a command line tool the minimal information it needs to be able to go parse your code, figure out the dependency map for itself, and generate some kind of definition to be used during runtime.  Minimal information might include some kind of seed information like where to find your classes or perhaps what filters to use when inspecting classes.  Sometimes, these tools might make use of special interfaces (also called interface injection) to understand that their purpose is to describe the various dependencies of the class implementing said interface.  Another approach might be to utilize special annotations on classes and class methods that describe the various required and optional dependencies and how they are to be injected.</p>
<p>The same techniques employed in this manual approach could also be put to use in an automatic approach.  In automatic approach, imagine this same command line tool from the manual approach was now a service of the application itself.  While in development mode, it would run as often as need be in order to determine if code changes have happened.  If they have, the service would regenerate the dependency definition file so that the rest of the application can utilize the dependency definition inside the DI container available to the application during runtime.</p>
<p>There are a couple of concerns that are specific to PHP with regards to dependency management.  Since PHP is a share-nothing architecture with no application level memory, this definition would need to be loaded and parsed and put into memory on each request.  The larger the dependency tree that you track, the larger the memory footprint of the dependency definition graph.  Furthermore, since this definition has to be loaded on each request, if it is in a non-native format (meaning anything other than PHP code), there are certain costs with converting this format, be it XML, YAML, JSON, or INI to the in-memory structure that the dependency management container requires.  What&#8217;s more, the PHP platform does not keep track of file changes.  So without some kind of user-land tracking, it is hard to know what files during development have changed.  Thus, your dependency management system, if it&#8217;s taking an automatic approach, would have to rescan the filesystem for changes upon each request during development &#8211; which has its own consequences.</p>
<h4>Deployment Time</h4>
<p>When one is done writing code and is ready to push this application into production, the act of pushing this application is called deployment.  The mode for this application is now considered &#8220;production&#8221;.  In production, you can be sure that the structure of your code is stable and will not change, thus your dependency graph is now safe from changes too.  Since this is the case, there is no longer a need to keep updating and regenerating this dependency definition file like you were during development.</p>
<p>Even though the definition is no longer changing, there still is the concern about how expensive it is to load this definition each request.  Naturally, the cheapest form of definition would be a PHP array or structure describing the definition that can then be loaded in-memory.  Other file types like XML, YAML, JSON, etc first have to go through a parsing phase before they can be used.  This activity of parsing these files could be expensive, and could benefit from some kind of caching.  Caching the definition in some way shape or form, would ensure there is minimal overhead per-request when the application is using this dependency management container.</p>
<h3>Other Observations &amp; Criticisms</h3>
<p>It is important to realize that dependency management solutions in and of themselves are, in all the available words, full frameworks.  They require that you understand both their philosophy as well have a minimal understanding of what facilities they are offering in order to use them effectively.  To understand the true benefits of any framework one must first know the pain points the framework is attempting to solve.  Seeing the end result of a framework without knowing what it is facilitating might lead to one to dismiss it as overkill or unintuitive.  For example, take the following code (typical of dependency management systems)</p>
<p></p><pre class="crayon-plain-tag">$userRepository = $dic-&amp;gt;get('UserRepository');</pre><p></p>
<p>If you encounter this line of code without fully understanding the dependency injection container being used, you wouldn&#8217;t be able to appreciate its usefulness.  You could instantiate your Application\Model\UserRepository yourself, sure, but you&#8217;d also have to locate and inject the database adapter to use and into that you&#8217;d have to inject and load the configuration for that database connection.  If you are doing this in multiple controller actions, there is a lot of repeated boilerplate code that is required to &#8220;wire&#8221; the UserRepository object.  Internally, the DiC object is loading and consulting a definition, creating objects, injecting those objects, and returning the requested object that has been fully wired and ready to use.</p>
<p>The above code also demonstrates two common criticism of dependency management frameworks, which is also a criticism of frameworks in general.  By using this framework, you are moving further away from the facilities of the language or platform itself.  Instead of using the &#8220;new&#8221; keyword to create a new object, you&#8217;ve asked another object to create this requested object for you.  What this has done has shifted developers away from utilizing the language&#8217;s well understood API and onto the framework&#8217;s API.  Additionally, this kind of code is not easily understood by IDE&#8217;s.  While special features could be added to the IDE to support this framework, it does not inherently know what kind of object is being returned by the $dic->get(..) method call.</p>
<h3>Summary</h3>
<p>While dependency management frameworks have clear drop-in benefits, there exist a few considerations that have unknown or unexplored consequences.  For example, if the benefit is such that all dependencies are managed, and all a developer has to do is configure it, does that encourage deeper object graphs when creating classes and class dependencies?  If so, what is the performance impact of these deep object graphs, particularly on the PHP platform.  What are the memory implications of such object graphs, what are the speed implications of them?  Furthermore, if one needed to debug an object that has been generated by a dependency management framework, is that easily possible?</p>
<p>At the end of the day, whether or not to use a dependency management framework is a matter of cost versus benefit.  In order to be able to make an informed decision, a developer should consider a few scenarios. First, one should know what code might look like with and without this new framework.  This will give an indication of the cost/benefit at the code level, does it actually save lines of code, and developer headaches?  Secondly, one should consider how much added knowledge a developer or a team of developers need in order to understand this framework. Lastly, one should consider what kind of performance impact implementing this new framework has on the application&#8217;s throughput.</p>
<div class="shr-publisher-144"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>PHP Component and Library API Design Overview</title>
		<link>http://ralphschindler.com/2011/01/18/php-component-and-library-api-design-overview</link>
		<comments>http://ralphschindler.com/2011/01/18/php-component-and-library-api-design-overview#comments</comments>
		<pubDate>Tue, 18 Jan 2011 22:48:00 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=134</guid>
		<description><![CDATA[There&#8217;s been lots of change in the PHP community over the past few years. PHP now has namespaces. More PHP developers are using an IDE. More PHP developers are pulling inspiration from the Java, C#/.NET, and Ruby communities. And even more PHP developers are embracing the object-oriented and, ironically, the functional nature (closures) of PHP. [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>There&#8217;s been lots of change in the PHP community over the past few years.  PHP now has namespaces.  More PHP developers are using an IDE.  More PHP developers are pulling inspiration from the Java, C#/.NET, and Ruby communities.  And even more PHP developers are embracing the object-oriented and, ironically, the functional nature (closures) of PHP.  All these changes make for interesting code.  What has also happened is that better and more readable code is being produced by this ever growing PHP community.  It&#8217;s been a long time since &#8220;PHP application&#8221; meant a series of <a href="http://martinfowler.com/eaaCatalog/transactionScript.html">transaction scripts</a> as a mix of SQL, CSS, JS, with some PHP sprinkled in, and a couple of few classes for good measure.  Of course, that still exists, but you no longer need to go to the ends of the earth to find non-spaghetti code that is understandable within a few minutes.</p>
<p>For the most part, all of these changes are good changes.  The number of good/senior/expert level PHP developers is ever increasing and there are more and more &#8220;enterprise grade&#8221; frameworks and libraries that are being produced.  That said, with all of these new changes, the one area which is still fairly inconsistent from project to project is the naming conventions that are employed inside PHP 5.3 project that utilize namespaces.  This article will attempt to describe what an API is, how names and object-oriented features affect an API, and how various decisions affect the consumers of a particular API is.</p>
<h4>What Is An API?</h4>
<p>Before we jump into naming, it&#8217;s important to have a common understanding of the actual problem area.  When we talk about names, we are really talking about the API.  <em>An API is</em> a particular set of rules and specifications that a developer can follow to access and make use of the services and resources provided by another particular software program, component or library.  Put another way, it is an interface between various software pieces and facilitates their interaction, similar to the way the user interface facilitates interaction between humans and computers.</p>
<p>For PHP 4 / procedural based libraries, the API is defined by the functions that are declared for usage in that library.  It is further described by the global names and global state that the library utilizes to do its job.  Typically, API&#8217;s based on purely function based libraries are far simpler to understand.</p>
<p>Object-oriented API&#8217;s are a bit more complex.  When you build an object-oriented library or component, you are typically designing <em>two</em> API&#8217;s at the same time, whether or not you know it.  This is the nature of object-oriented languages when you employ the use of abstract classes and interfaces in your design.</p>
<p>The first API, the more common of the two, I call the <em>Consumption API</em>.  This is the API that answers the question: &#8220;how do people consume this thing.&#8221;  The answer to this question is generally situated around the great majority of use cases that were identified by the author of the software component/library.  In PHP, consumption might look like this:</p>
<p></p><pre class="crayon-plain-tag">$foo = new SomeCompany\FooComponent\FooComponent($options);
$foo-&amp;gt;setAdapter(new SomeCompany\FooComponent\Adapter\SomeAdapter($adapterOptions));
$interestingResult = $foo-&amp;gt;doSomethingInteresting();</pre><p></p>
<p>As you can see, <em>no</em> declarative code was required to fulfill the most common use case that was identified as a need for this component&#8217;s existence.  The above API is defined by the totality of all the public (concrete) classes, their public properties and public methods.  By examining these elements, a good API design  should allow a developer to deduce how the component works without examining <em>any</em> documentation.  When that is possible, the API has become the documentation as well as the &#8220;story&#8221; behind how the component/library is to be used.</p>
<p>Not all use cases are accounted for in generic components and generic libraries.  As developers, we attempt to create generic libraries and components that will solve the majority of problems of the majority of the community.  We cannot envision all use cases or even edge cases behind a particular component.  That said though doesn&#8217;t means that the outlying use cases are unimportant or should be unaccounted for.  These use cases are handled by the secondary API: the <em>&#8220;Extension API&#8221;</em>.</p>
<p>The Extension API answers the question: &#8220;since this component does 90% of what I want, how can I extend it to fulfill the last few of my needs.&#8221;  Clearly, it makes sense to leverage tools that do most of what you need especially if they can be extended in ways that are outside of the out-of-the-box feature-set.  Object-oriented/class based code is particularly well suited to extension through the principle of <i>overriding polymorphism</i>.</p>
<p>The primary tool behind overriding polymorphism is method overriding.  For this to be possible, base types, or the types that are shipped with the component/library you are extending, will be overridden to fulfill this new behavior that is your specialized use case.  Consider the following code example:</p>
<p></p><pre class="crayon-plain-tag">namespace MyCompany\FooComponent\Adapter; // My Component
use SomeCompany\FooComponent\Adapter\SomeAdapter; // Consumed Component

// extend the provided Component with my special use case
class MyAdapter extends SomeAdapter
{
    protected function _someWorkToBeDone()
    {
        // do something special that fulfills our use case
        return parent::_someWorkToBeDone();  // protected method on parent class
    }
}</pre><p></p>
<p>As you can see here, we&#8217;ve extended the functionality of the base adapter from the shipped component/library with our own functionality.  This is possible since the base adapter tucked away the business logic we needed to alter inside a <em>protected</em> method.  This is what allows us to rely on overriding polymorphism to extend code to suit more specific needs.  This &#8220;Extension API&#8221; can therefore be defined by the totality of all <em>protected</em> members of a class: methods and properties that can be utilized in child classes.  These protected methods are not all that important or even useful in the documented and de-facto use cases of a component, but become extremely important when extending.</p>
<h4>API Philosophy</h4>
<p>It&#8217;s hard to quantify importance of any one aspect of a codebase&#8217;s API over another without first talking about the general philosophy.  In the land of a 1000 frameworks and libraries, being well written and poorly written divides the great majority of them.  Of what is left of the (generally regarded) well written ones, philosophy divides the rest.</p>
<p>There exist two common philosophical &#8220;goals&#8221; that most libraries/components generally subscribe to that, depending on your perspective, might be contradictory.  For arguments sake, let&#8217;s assume that each is as important as the other.  The first: &#8220;easy to use&#8221;.  A component&#8217;s like-ability by developers is greatly determined by how easy something is to use, if it&#8217;s intuitive, if it&#8217;s fulfills the majority of one&#8217;s needs.  The other: &#8220;easy to extend&#8221;.  The majority of the time, a component is written for some well known use cases.  Generally, that will suite the majority of the needs of any one developer, but there are always some unknown use cases.  A components ability to be able to deliver a mostly working solution while allowing the developer to extend it for the unknown is what determined how easy it is to extend said component.</p>
<p>More often than not, ease of use and extensibility live at two ends of the spectrum.  Things that are easy to use are generally hard to extend, and things that are simple to extend are generally harder to use.  This is the case because to accommodate one usually comes at the expense of the other.</p>
<p>Getting back to philosophy and this example at hand, both ease of use and extensibility are both equally important.  The goal, in terms of API design, is to be able to accommodate each equally and strike a balance between the two so that each goal is represented in the API.</p>
<h4>Basic Tips And Tricks For Better APIs</h4>
<p>The tips and tricks for building better component API&#8217;s could get fairly long, so this article will attempt to cover some of the more &#8220;basic&#8221; ideas.</p>
<h5>Adopt A Common Namespace &amp; Class Naming Scheme</h5>
<p>While it is true that the PHP platform has no built-in packaging, or file based import mechanism&#8230; the PHP autoloader with the help of some common conventions can get you 99% of the way there.  Large projects like Zend Framework, Symfony, PHPUnit, and PEAR have all settled on a pretty simple and common naming scheme based on the PEAR naming standards.  By utilizing this naming scheme, your code will be instantly familiar to developers who already have knowledge of this scheme in other projects.  The benefit here is that developers will know exactly where to find classes inside the filesystem.</p>
<p></p><pre class="crayon-plain-tag">namespace MyCompany\MyComponent;
class Foo {
    // will be found relative to the include_path, or some path
    // managed by an autoloader at
    // MyCompany/MyComponent/Foo.php, pretty simple eh?
}</pre><p></p>
<h5>Avoid Doing Too Much In the Constructor</h5>
<p>There&#8217;s lots of places on the web that discuss this, so I&#8217;ll <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">link</a> to <a href="http://usrportage.de/archives/897-Antipattern-the-verbose-constructor.html">them</a> <a href="http://www.beaconhill.com/kb/java/code-in-constructor-anti-pattern.html">here</a> and not go into too much detail.  I&#8217;ve seen it called a &#8220;unified constructor&#8221;, but that&#8217;s not what we are talking about here, or at least, that is not the goal.  The goal is to allow the consumer to give as much or as little information about the identity of the object at instantiation time.  The common signature that I like for this is the following:</p>
<p></p><pre class="crayon-plain-tag">class Foo
{
    public function __construct($options = null)
    {
        if (is_array($options)) {
            $this-&amp;gt;setOptions($options);
        } elseif (is_string($options)) {
            $this-&amp;gt;setValueThatIsDocumentAndWellKnown($options);
        }
    }
}</pre><p></p>
<p>Generally, the call to setOptions() will in turn call various setters if they exist.  What is important is that at construction/instantiation time a consumer is not required to fulfill all of the classes requirements.  Why is this important? It reverses order in which dependencies are required to be interacted with.  Lets examine this in code:</p>
<p></p><pre class="crayon-plain-tag">// Example 1
// assuming: class Foo { __construct(A $a, B $b, C $c) {} }
$a = new A($aOption1, $aOption2);
$b = new B();
$c = new C($cOption, $a);

$foo = new Foo($a, $b, $c); // and finally
$foo-&amp;gt;doSomethingInteresting();

/** OR ALTERNATIVELY **/

// Example 2
// assuming: class Foo { __construct($options = null) {} }
$foo = new Foo(array(
    'a' =&amp;gt; ($a = new A($aOption1, $aOption2)),
    'b' =&amp;gt; new B(),
    'c' =&amp;gt; new C($cOption, $a)
    ));
$foo-&amp;gt;doSomethingInteresting();

// Example 3
// or better:
$foo = new Foo();
$a = new A($aOption1, $aOption2);
$foo-&amp;gt;setA($a)
    -&amp;gt;setB(new B())
    -&amp;gt;setC(new C($cOption, $a));
$foo-&amp;gt;doSomethingInteresting();</pre><p></p>
<p>The difference is that in Example 1, even though our target use case is handled by class <tt>Foo</tt>, we are forced to interact with the dependencies first.  Conversely, examples 2 and 3 show that our target object <tt>Foo</tt> is created up front, and dependencies are handled after instantiation.  If code clarity is a goal, reading the code top down in example 2 and 3 makes more sense than in example 1 since the API has allowed the developer to code his use case in a top-down or story-like code block.  Why do I like this pattern of usage? Simple: it highlights PHP&#8217;s loose nature and flexibility in it&#8217;s use case&#8230; but mostly because it&#8217;s more readable.</p>
<h5>Avoid <tt>final</tt> And <tt>private</tt></h5>
<p>This one speaks to extensibility.  Unless you are attempting to restrict a user from utilizing some kind of use case, there is little gain in marking members as <tt>final</tt> or <tt>private</tt>.  Sooner or later, someone somewhere will need to override a method you&#8217;ve implemented for some obscure use case.  A better approach is to provide them with a codebase that will meet most of their needs and can be extended to fulfill the rest if they are outside the original scope.  That way, they are not forced to patch your codebase.</p>
<h4>Summary</h4>
<p>This is by far <em>not</em> an exhaustive list.  As more of the larger projects move to using namespaces, closures and the other PHP 5.3 features, we&#8217;ll start to see a few more best-practices emerge as they relate to API design.  In the mean time, this overview will serve as a springboard for a few discussions on API design moving forward with ZF2 and PHP 5.3 component development that is currently on-going.</p>
<div class="shr-publisher-134"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2011/01/18/php-component-and-library-api-design-overview/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Composite Rowsets For Many-To-Many Relationships Via Zend_Db_Table</title>
		<link>http://ralphschindler.com/2010/11/15/composite-rowsets-for-many-to-many-relationships-via-zend_db_table</link>
		<comments>http://ralphschindler.com/2010/11/15/composite-rowsets-for-many-to-many-relationships-via-zend_db_table#comments</comments>
		<pubDate>Tue, 16 Nov 2010 01:30:24 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Db]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=120</guid>
		<description><![CDATA[One of the hardest problems to solve when developing an ORM of any complexity is in deciding how to handle the retrieval of rows that satisfy a many-to-many relationship, also known as a M:N relationship. From the perspective of an object, there is no such thing as a many to many relationship. There are only [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>One of the hardest problems to solve when developing an ORM of any complexity is in deciding how to handle the retrieval of rows that satisfy a <a href="http://en.wikipedia.org/wiki/Many-to-many_(data_model)">many-to-many relationship</a>, also known as a M:N relationship.  From the perspective of an object, there is no such thing as a many to many relationship.  There are only two relationships an object understands.  The first is the relationship of itself to another object, which is a one to one (1:1) relationship. The second is the relationship of itself to a group of other objects, or a one-to-many (1:N) relationship.  It&#8217;s not until you look at the relationship of all objects in a system that the many-to-many relationship pattern emerges.</p>
<p>In RDBM systems, rows and their relationships are modeled through the use of foreign keys and foreign key constraints between a left table and a right table.  Foreign key constraints, by themselves, can only model 1:1 and 1:N relationship of rows.  To model M:N relationships, database developers must get creative.  By employing the use of a &#8220;3rd party&#8221;, and by utilizing foreign keys that model a 1:N relationship, database developers can model a M:N relationship.  This 3rd party comes in the form of another table that may or may not have any data model specific information attached to it.  This table is generally known as a <a href="http://en.wikipedia.org/wiki/Junction_table">junction table</a>, but has also been known as a cross-reference table, bridge table, join table, map table, intersection table, linking table, many-to-many resolver, link table, or association table.</p>
<h3><tt>Zend_Db_Table_Row</tt> And Junction Tables</h3>
<p><tt>Zend_Db_Table</tt> is a component in Zend Framework that implements the <a href="http://martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data</a> and <a href="http://martinfowler.com/eaaCatalog/rowDataGateway.html">Row Data Gateway</a> patterns. In short, a row object attempts to create a single PHP object per actual row in the database table.  Furthermore, <tt>Zend_Db_Table_Row</tt> objects can go as far as to describe, understand, and interrogate these various 1:1, 1:N and M:N relationships.  This allows row objects to be able to find and return related row objects in the form of a <i>rowset</i>.</p>
<p>One of the primary tenets of <tt>Zend_Db_Table</tt> and <tt>Zend_Db_Table_Row</tt> is to be able to produce consistent row objects.  This means that the properties of these row objects should be a <i>complete</i> and logical representation of how the row might look inside the table of the RDBMS.</p>
<p>Some time ago an issue (<a href="http://framework.zend.com/issues/browse/ZF-6232">ZF-6232</a>) was filed against <tt>Zend_Db_Table</tt> to report that columns from the junction table were being included in the resulting rowset&#8217;s row objects.  This was causing issues for people who then attempted to <tt>save()</tt> the row object back to the database.  If a developer mistakenly altered one of the junction table values that was accidentally included in the row, <tt>Zend_Db_Table_Row</tt> would throw an exception since the row object had more columns than the actual row in the database.  Given that we want to create consistent, complete and logical row objects, a solution was devised to ensure that the junction table&#8217;s row information was not included in the resulting rowset&#8217;s rows.  Consequently, this meant that anyone relying on this undocumented behavior would no longer be able to get data stored inside the junction table as part of the result set&#8217;s row object.  This fix was incorporated into the 1.10.2 release.</p>
<p>Over the past several years of working on Zend Framework, I&#8217;ve noticed the developer population at large is <em>really</em> good at finding undocumented and previously unthought-of use-cases of Zend Framework components.  These use-cases, while sometimes &#8220;inventive&#8221; to say the least- are also sometimes blatant misuses of a component.  It suffices to say that these use-cases are not captured in a unit test and consequently are not protected by backwards compatibility.</p>
<p>Relying on <tt>Zend_Db_Table_Row</tt> to include junction data is not only an unintended use case but also a misuse of the <tt>findManyToManyRowset()</tt> functionality provided by <tt>Zend_Db_Table_Row</tt>.  That said, I do want to provide a solution for developers that relied on this behavior of Zend_Db_Table_Row in Zend Framework previous to 1.10.2.</p>
<h3>A Solution</h3>
<p>While the motivation for creating this class is based on providing a solution to developers who relied on utilizing junction table data in Zend_Db_Table_Row&#8217;s many-to-many rowsets, this same technique can be utilized with any ORM or database abstraction layer that handles many-to-many result sets.</p>
<p>Basically, I&#8217;ve created a single class that effectively take the place of <tt>Zend_Db_Table_Row::findManyToManyRowset()</tt> for the purposes of creating an iterable rowset that allows access to both the target many-to-many rowset as well as the junction rowset.  This solution is called a <em>Composite Rowset</em>.  In this solution, both rowsets (iterators) are kept in sync with one another.  This proves to be an ideal solution in a couple of ways.  First, it will produce consistent row objects that are explicitly tied to a row in a database.  Second, the cost of creating this composite rowset is at the expense of 2 queries: the original many-to-many query and a similar query to retrieve the junction rowset.  This is ideal since previously, to get the junction data, <tt>findDependentRowset()</tt> would have had to been called on each row within the rowset produced by the <tt>Zend_Db_Table_Row::findManyToManyRowset()</tt>.</p>
<p>The API for this Composite Rowset looks like this:</p>
<p></p><pre class="crayon-plain-tag">/**
 * @link https://github.com/gooeylabs/Gooey-PHP-5.2-Components/blob/master/library/Gooey/Db/Table/ManyToManyCompositeRowset.php
 */
class Gooey_Db_Table_ManyToManyCompositeRowset implements SeekableIterator, ArrayAccess, Countable
{

    public function __construct(Zend_Db_Table_Row_Abstract $row, $matchTableName, $junctionTableName, $matchRefRule = null);
    public function seek($position);
    public function current();
    public function currentJunction();
    public function next();
    public function rewind();
    public function key();
    public function valid();
    public function offsetSet($offset, $value);
    public function offsetGet($offset);
    public function offsetExists($offset);
    public function offsetUnset($offset);
    public function count();
    public function getRow($position, $seek = false);
    public function getJunctionRow($position, $seek = false);
    public function toArray();
    public function junctionRowsetToArray();    
}</pre><p></p>
<p>NOTE: Full class located <a href="https://github.com/gooeylabs/Gooey-PHP-5.2-Components/blob/master/library/Gooey/Db/Table/ManyToManyCompositeRowset.php">here.</a></p>
<p>As you can see, the API mirrors that of <tt>Zend_Db_Table_Rowset</tt> to provide a something that is immediately recognizable. Below is an example of sample usage.  For this example, assume there is a typically artist/genre data model that demonstrates a many-to-many relationship.  Inside of the junction table we are attempting to track the date that the relationship was created.  This examples shows this usage:</p>
<p></p><pre class="crayon-plain-tag">$aTable = new ArtistTable();
$artist1 = $aTable-&amp;gt;find(1)-&amp;gt;current();
echo 'Artist: ' . $artist1-&amp;gt;name . PHP_EOL;
// instead of $genres = $a-&amp;gt;findManyToManyRowset('GenreTable', 'ArtistGenreTable');
$genres = new Gooey_Db_Table_ManyToManyCompositeRowset($artist1, 'GenreTable', 'ArtistGenreTable');

// iterate
foreach ($genres as $genre) {
    echo '  Genre ' . $genre-&amp;gt;name . ' added on ' . $genres-&amp;gt;currentJunction()-&amp;gt;added_on . PHP_EOL;
}

/** 
 * Sample Output:
 *
 *    Artist: Foo Artist
 *      Genre Rock &amp;amp; Roll added on 2010-11-10
 *      Genre Hiphop added on 2010-11-11
 *
 */</pre><p></p>
<h3>Where To Get It &amp; Conclusions</h3>
<p>This code is available on my <a href="https://github.com/gooeylabs">GooeyLabs</a> github account, specifically inside of the <a href="https://github.com/gooeylabs/Gooey-PHP-5.2-Components">Gooey-PHP-5.2-Components</a> repository.  (Gooey is my namespace and moniker for my open source code contributions.)  Hopefully, those who have found they&#8217;ve had issues with the above mentioned fix for <tt>Zend_Db_Table_Row::findManyToManyRowset()</tt> and junction table data might find value in this class.</p>
<div class="shr-publisher-120"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2010/11/15/composite-rowsets-for-many-to-many-relationships-via-zend_db_table/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Exception Best Practices in PHP 5.3</title>
		<link>http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3</link>
		<comments>http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3#comments</comments>
		<pubDate>Wed, 15 Sep 2010 20:21:18 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=107</guid>
		<description><![CDATA[Every new feature added to the PHP runtime creates an exponential number of ways developers can use and abuse that new feature-set. However, it’s not until developers have had that chance that some agreed-upon good usage and bad usage cases start to emerge. Once they do emerge, we can finally start to classify them as [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Every new feature added to the PHP runtime creates an exponential number of ways developers can use and abuse that new feature-set. However, it’s not until developers have had that chance that some agreed-upon good usage and bad usage cases start to emerge. Once they do emerge, we can finally start to classify them as best or worst practices.</p>
<p>Exception handling in PHP is not a new feature by any stretch.  In this article, we&#8217;ll discuss two new features in PHP 5.3 based around exceptions.  The first is nested exceptions and the second is a new set of exception types offered by the SPL extension (which is now a core extension of the PHP runtime).  Both of these new features have found their way into the book of best best practices and deserve to be examined in detail.</p>
<p>Special note: some of these features have existed in PHP &lt; 5.3 or are at least capable of being implemented in PHP &lt; 5.3.  When this article mentions PHP 5.3, it is not in the strictest sense of the PHP runtime.  Instead, it is meant that code bases and projects that are adopting PHP 5.3 as a minimum version but also all of the best practices that have emerged in this new phase of development.  This phase of development highlighted by the &#8220;2.0&#8243; efforts of projects like Zend Framework, Symfony, Doctrine and PEAR to name a select few.</p>
<h3>Background</h3>
<p>Previously in PHP 5.2, there was a single exception class Exception.  Generally, speaking from a Zend Framework / PEAR coding standard perspective, this exception class became the root for all exceptions that might be thrown from within your library.  For example, if you created a library for your company MyCompany, then you would, according to ZF/PEAR standards, have prefixed all code with <tt>MyCompany_</tt>.  For this library, you might create a base exception for your library code: <tt>MyCompany_Exception</tt>, which extends the PHP class <tt>Exception</tt> and from which all your components might inherit, subclass, and throw.  So, if you created a component <tt>MyCompany_Foo</tt>, it might have a base exception class called <tt>MyCompany_Foo_Exception</tt> that is expected to be thrown from within the <tt>MyCompany_Foo</tt> component.  These exceptions can be caught by attempting to <i>catch</i> <tt>MyCompany_Foo_Exception</tt>, <tt>MyCompany_Exception</tt>, or simply <tt>Exception</tt>.  This would allow 3 levels of granularity (or more depending on how many times the <tt>MyCompany_Foo_Exception</tt> was subclassed) to consumers of this component in this particular library, and handle that exception in a way they deem fit.</p>
<h3>New Feature: Nesting</h3>
<p>In PHP 5.3, the base exception class now <a href="http://us3.php.net/manual/en/language.exceptions.extending.php">handles <i>nesting</i></a>.  What is nesting? Nesting is the ability to catch a particular exception, create a new exception object to be thrown with a reference to the original exception.  This then allows the caller access to both the exception thrown from within the consumed library of the more well known type, but also access to the exception that originated this exceptional behavior as well.</p>
<p>Why is this useful?  Typically, this is most useful in code that consumes other code that throws exceptions of its own type.  This might be code that utilizes the <A href="http://en.wikipedia.org/wiki/Adapter_pattern">adapter pattern</a> to wrap 3rd party code to deliver some kind of adaptable functionality, or simply code that utilizes some exception throwing PHP extension.</p>
<p>For example, in the component <a href="http://framework.zend.com/manual/en/zend.db.html">Zend_Db</a>, it uses the adapter pattern to wrap specific PHP extensions in order to create a database abstraction layer.  In one adapter, <tt>Zend_Db</tt> wraps PDO, and PDO throws its own exception <tt>PDOException</tt>, <tt>Zend_Db</tt> needs to catch these PDO specific exceptions and re-throw them as the expected and known type of <tt>Zend_Db_Exception</tt>.  This gives developers the assurance that <tt>Zend_Db</tt> will always throw exceptions of type <tt>Zend_Db_Exception</tt> (so it can be caught), but they will also have access to the original PDOException that was thrown in case it is needed.</p>
<p>The following is an example of how a fictitious database adapter might implement nested exceptions:</p>
<p></p><pre class="crayon-plain-tag">class MyCompany_Database
{
    /**
     * @var PDO object setup during construction
     */
    protected $_pdoResource = null;
    
    /**
     * @throws MyCompany_Database_Exception
     * @return int
     */
    public function executeQuery($sql)
    {
        try {
            $numRows = $this-&amp;gt;_pdoResource-&amp;gt;exec($sql);
        } catch (PDOException $e) {
            throw new MyCompany_Database_Exception('Query was unexecutable', null, $e);
        }
        return $numRows;
    }

}</pre><p></p>
<p>To utilize a nested exception, you would call the <tt>getPrevious()</tt> method of the caught exception:</p>
<p></p><pre class="crayon-plain-tag">// $sql and $connectionParameters assumed
try {
    $db = new MyCompany_Database('PDO', $connectionParams);
    $db-&amp;gt;executeQuery($sql);
} catch (MyCompany_Database_Exception $e) {
    echo 'General Error: ' . $e-&amp;gt;getMessage() . &amp;quot;\n&amp;quot;;
    $pdoException = $e-&amp;gt;getPrevious();
    echo 'PDO Specific error: ' . $pdoException-&amp;gt;getMessage() . &amp;quot;\n&amp;quot;;
}</pre><p></p>
<p>Most recent PHP extensions have OO interfaces.  As such, those API&#8217;s tend to lean on throwing exceptions instead of raising errors.  A short list of exception throwing extensions in PHP include PDO, DOM, Mysqli, Phar, Soap and SQLite.</p>
<h3>New Feature: New Core Exception Types</h3>
<p>Also in PHP 5.3 development we are shining a light on some new and interesting Exception types.  These exceptions have been in place since the PHP 5.2.x, but it has not been till recently and the &#8220;re-evaluation&#8221; exception best practices that they are now gaining some limelight.  They are implemented in the SPL extension and are listed on the manual pages located <a href="http://us.php.net/manual/en/spl.exceptions.php">here</a>.  Since these new exception types are part of core PHP as part of SPL, they can be used by anyone who targets PHP 5.3 as the minimum runtime for their code.  While this might seem less important for when writing application layer code, the way we adopt and use these new exception types becomes even more important when we are writing and consuming library code.</p>
<p>So why new exception types in general?  Previously, developers attempted to give more meaning to their exceptions by putting more information into the message of the exception.  While this is good, it has a few drawbacks.  One is that you cannot catch an exception based on a message.  This can be a problem if you know a set of code is throwing the same exception type with various message for various exceptional conditions that can be handled differently.  For example, an authentication class that during <tt>$auth->authenticate();</tt> it throws the same type of exception (let&#8217;s assume Exception), but with different messages for two specific failures: a failure where the authentication server cannot be reached and the same exception type but different message for a failed authentication attempt.  In this case (nevermind that using Exceptions might not be the best way to handle authentication responses), it would require string parsing the message to handle those two scenarios differently.</p>
<p>The solution to this is clearly some way to codify exceptions so that they can be easily interrogated when trying to discern how to react to this exceptional situation.  The first response libraries have had is to use the <tt>$code</tt> property of the Exception base class.  The other is to create multiple types, or new exception classes, that can be thrown to describe the behavior.  Both of these approaches have the same simple drawback.  Neither has emerged as a best practice and as such, neither is considered a standard, thus each project attempting to replicate this solution might do so with small variations that force the consumer to go back to the documentation to understand the library specific solution that was created.  Now with the new types approach in the SPL, otherwise known as the <i><b>Standard</b> PHP Library</i>; developers can utilize these new types in the same way in their projects and the projects they are consuming since a best practice for these new types has emerged.</p>
<p>The second drawback of the detailed message approach is that it makes understanding the exceptional situation harder for non-english or limited-english speaking developers.  This might slow down some developers when trying to decipher what an exception message is trying to convey.  As many developers as there are writing exceptions, there are equally as many variations in how they will describe that situation in the message since there is no standard for conformity or for codification.</p>
<h4>So How Do I Use Them, Give Me The Dirty Details?</h4>
<p>There are a total of 13 new exceptions in the SPL.  Two of them can be considered &#8220;base&#8221; types: <tt>LogicException</tt> and <tt>RuntimeException</tt>; both extend the PHP <tt>Exception</tt> class.  The remainder of the methods can thusly be broken down into three logical groups: the <em>dynamic call</em> group, the <em>logic</em> group and the <em>runtime</em> group.</p>
<p>The <em>dynamic call</em> group contains the exceptions <tt>BadFunctionCallException</tt> and <tt>BadMethodCallException</tt>.  <tt>BadMethodCallException</tt> is a subclass of <tt>BadFunctionCallException</tt> which in turn is a subclass of <tt>LogicException</tt>.  That means that these exceptions can be caught by either their direct type, <tt>LogicException</tt>, or simply <tt>Exception</tt>.  When do you use these?  Generally, these should be used when an exceptional situation arises as a result of an unresolvable __call() during a method or when a callback cannot find a valid function to call (or better put, when something is not <tt>is_callable()</tt>).</p>
<p>For example:</p>
<p></p><pre class="crayon-plain-tag">// OO variant
class Foo
{
    public function __call($method, $args)
    {
        switch ($method) {
            case 'doBar': /* ... */ break;
            default:
                throw new BadMethodCallException('Method ' . $method . ' is not callable by this object');
        }
    }

}

// procedural variant
function foo($bar, $baz) {
    $func = 'do' . $baz;
    if (!is_callable($func)) {
        throw new BadFunctionCallException('Function ' . $func . ' is not callable');
    }
}</pre><p></p>
<p>While the direct example is inside <tt>__call</tt> and anywhere near something that will <tt>call_user_func()</tt>, this group of exceptions are also useful when developing any kind of API where dynamic method call and function call lookups are utilized.  An example of this would be a SOAP or XML-RPC client/server who is capable of issuing and/or interpreting method requests.</p>
<p>The second group is the <em>logic</em> group.  This group consists of <tt>DomainException</tt>, <tt>InvalidArgumentException</tt>, <tt>LengthException</tt>, and <tt>OutOfRangeException</tt>.  These exceptions are a subclass of <tt>LogicException</tt> which is in turn a subclass of the PHP <tt>Exception</tt> class.  You use these exceptions when there is an exceptional situation that arises from either a mutation of state or as a result of bad method or function parameters.  To get a better understanding of this, we will first look at the last group of exceptions.</p>
<p>The final group is the <em>runtime</em> group.  It consists of <tt>OutOfBoundsException</tt>, <tt>OverflowException</tt>, <tt>RangeException</tt>, <tt>UnderflowException</tt>, and <tt>UnexpectedValueException</tt>.  These exceptions are a subclass of <tt>RuntimeException</tt> which is in turn a subclass of the PHP <tt>Exception</tt> class.  These exception should be used when an exceptional situation arises during the &#8220;runtime&#8221; of a function or method call.</p>
<p>How do these logic group and runtime group work together?  If you look at the anatomy of an object, one of two things is generally happening. First, the object will be tracking and mutating state. This means the object is generally not doing anything (yet); it might have configuration passed to it; it might be setting up properties (via setters and getters); or, it might be getting references to other objects. Second, when the object is not tracking and mutating state, it is operating – doing what it was designed to do. This is the object’s <i>runtime</i>.  For instance, during the objects lifetime, it might be created, passed a configure object, then it might have setFoo($foo), setBar($bar) called.  During these times any kind of <tt>LogicException</tt> should be raised.  In addition, when the object is asked to do something, with parameters, for example $object->doSomething($someVariation);  during the first few lines when it interrogates that $someVariation variable, it would throw a <tt>LogicException</tt>.  After it is done interrogating $someVariation, and it goes on about doing its job of doSomething(), this is considered its &#8220;runtime&#8221; and in this code it would throw <tt>RuntimeExcpetion</tt>s.</p>
<p>To better understand, we&#8217;ll look at this concept in code:</p>
<p></p><pre class="crayon-plain-tag">class Foo
{
    protected $number = 0;
    protected $bar = null;

    public function __construct($options)
    {
        /** this area throws LogicException types **/
    }
    
    public function setNumber($number)
    {
        /** this method throws LogicException types **/
    }
    
    public function setBar(Bar $bar)
    {
        /** this method throws LogicException types **/
    }
    
    public function doSomething($differentNumber)
    {
        if ($differentNumber != $expectedCondition) {
            /** this area throws LogicException types **/
        }
        
        /**
         * From here on down, this method throws
         * RuntimeException types
         */ 
    }

}</pre><p></p>
<p>Now that this concept is understood, what does this do for a consumer of this code base?  The caller can be sure that anytime they are mutating the state of an object, they can catch exceptions with the most specific type, for example <tt>InvalidArgumentException</tt> or <tt>LengthException</tt>, and at least <tt>LogicException</tt>.  By having this level of granularity, and multiple types involved, they can catch the exception minimally with LogicException, but also get greater understanding of what when wrong via the actual type of the exception.  This same concept applies for the Runtime group of exceptions as well, more specific types can be thrown and either the specific or the less specific type will be caught.  This offers a greater deal of knowledge about the situation and granularity of control to the caller.</p>
<p>Below is a table of the information you might find of interest concerning these SPL exceptions</p>
<h3>Best Practices In Library Code</h3>
<p>Since the advent of these new exception types in PHP 5.3, a new best practice for library code has also emerged.  While it is most beneficial to get a standard specialized exception type like <tt>InvalidArgumentException</tt> or <tt>RuntimeException</tt>, it would also be useful to catch component level exceptions.  You can read a more in-depth discussion of the concepts on the <a href="http://framework.zend.com/wiki/display/ZFDEV2/Proposal+for+Exceptions+in+ZF2?showComments=false">ZF2 wiki</a> or the <a href="http://wiki.php.net/pear/rfc/pear2_exception_policy">PEAR2 wiki</a>.</p>
<p>The long and short of this, in addition to the best practices listed above, is that there should be a component level type that can be caught for any exception that emanates.  This is accomplished by using what is known as a <a href="http://en.wikipedia.org/wiki/Marker_interface_pattern">Marker Interface</a>.  By creating a component level marker interface, real exception types inside a given component can extends the SPL exception types and be caught by any number of class types at runtime.  Let&#8217;s examine the following code:</p>
<p></p><pre class="crayon-plain-tag">// usage of bracket syntax for brevity
namespace MyCompany\Component {

    interface Exception
    {}

    class UnexpectedValueException 
        extends \UnexpectedValueException 
        implements Exception
    {}

    class Component
    {
        public static function doSomething()
        {
            if ($somethingExceptionalHappens) {
                throw new UnexpectedValueException('Something bad happened');
            }
        }
    }

}</pre><p></p>
<p>Assuming the above code, if one were to execute <tt>MyCompany\Component\Component::doSomething()</tt>, the exception that is emitted from the <tt>doSomething()</tt> method can be caught by any of the following types: PHP&#8217;s <tt>Exception</tt>, SPL&#8217;s <tt>UnexpectedValueException</tt>, SPL&#8217;s <tt>RuntimeException</tt> the component&#8217;s <tt>MyCompany\Component\UnexpectedValueException</tt>, or the component&#8217;s <tt>MyCompany\Component\Exception</tt>.  This affords the caller any number of opportunities to catch an exception that emanates from a given component within your library.  Furthermore, by analyzing the types that make up the exception, more semantic meaning can be given to the exceptional situation that just occurred.</p>
<h3>Summary</h3>
<p>In summary, this article should help guide you in creating and throwing more meaningful exceptions in a standards based and best practices way by negating the emphasis of the exception message and putting more emphasis on the exception type.  If you&#8217;d like to carry on the discussion of these concepts feel free to comment here, on the PHP documentation pages, or in the ZF2 wiki comments section for the Exception proposal linked above.</p>
<div class="shr-publisher-107"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Compiling Gearman (or anything) for Zend Server CE on Snow Leopard</title>
		<link>http://ralphschindler.com/2010/05/12/compiling-gearman-or-anything-for-zend-server-ce-on-snow-leopard</link>
		<comments>http://ralphschindler.com/2010/05/12/compiling-gearman-or-anything-for-zend-server-ce-on-snow-leopard#comments</comments>
		<pubDate>Wed, 12 May 2010 21:58:33 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=98</guid>
		<description><![CDATA[The first thing you need to know about Mac OS.X Snow Leopard all Mac&#8217;s and Macbook Pro&#8217;s is that this hardware is 64 bit capable. This may not mean you are running a 64 bit kernel, it simply means that the operating system is capable of executing x86 64bit executables. We won&#8217;t go into the [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>The first thing you need to know about Mac OS.X Snow Leopard all Mac&#8217;s and Macbook Pro&#8217;s is that this hardware is 64 bit capable.  This may not mean you are running a 64 bit kernel, it simply means that the operating system is capable of executing x86 64bit executables.  We won&#8217;t go into the details of kernel architecture, you can read more about that <a href="http://www.osnews.com/story/22009/Snow_Leopard_Seeds_Use_32bit_Kernel_Drivers_by_Default">here</a>.</p>
<p>What is important though is that both x86_64 and i386 based executables can run on snow leopard.  What is not uncommon on OS.X is to have executables (and libraries) that have multiple architectures compiled in.  To see what architectures are inside a particular file, run something like this:</p>
<p>[sourcecode language="text" highlight="1,7"]<br />
    /usr/local# file /usr/bin/php<br />
    /usr/bin/php: Mach-O universal binary with 3 architectures<br />
    /usr/bin/php (for architecture x86_64): Mach-O 64-bit executable x86_64<br />
    /usr/bin/php (for architecture i386):   Mach-O executable i386<br />
    /usr/bin/php (for architecture ppc7400):        Mach-O executable ppc</p>
<p>    /usr/local# file /usr/local/zend/apache2/bin/httpd<br />
    /usr/local/zend/apache2/bin/httpd: Mach-O executable i386<br />
[/sourcecode] </p>
<p>This means that PHP (supplied by apple), has been compiled with 3 architectures inside.  What does that mean? It means there is basically 3 versions on PHP compiled into a single binary, and that when it is loaded into memory, only one particular version will be used at a time.  To demonstrate, lets take a pretty common difference between 32bit and 64bit architectures: integer size.  We know that 64 bit integer space is larger than that of the 32bit space.  The following demo will show running different architectures from the same binary:</p>
<p>[sourcecode language="text" highlight="1,4"]<br />
    /usr/local# arch -arch x86_64 /usr/bin/php -nr &#8216;echo PHP_INT_MAX;&#8217;<br />
    9223372036854775807</p>
<p>    /usr/local# arch -arch i386 /usr/bin/php -nr &#8216;echo PHP_INT_MAX;&#8217;<br />
    2147483647<br />
[/sourcecode]</p>
<p>We know we are running same command though different architectures since we know PHP has different max integer sizes.</p>
<p>The next important thing to understand is the nature of the PHP stack.  PHP is generally regarded as a glue language.  That might mean several things to different people, but we will be looking strictly at this statement in the purest technical sense.  PHP is made of the core language and features, but also a rich set of extensions.  These extensions are typically written in C, and have interfaced with the C layer PHPAPI.  Most of the really useful extensions are linked against libraries on your system, for example the openssl set of functions are not actually implemented in PHP&#8217;s source code, the openssl extension is simple a wrapper that calls out to libssl.so (or .dylib on mac, .dll on windows).  This is what is meant by PHP being a glue language/platform.</p>
<p>Since PHP relies on existing compiled libraries, you further have to understand how things are linked and compiled.  There are generally two options here: linking dynamically, or statically compiling.  Either way, one thing remains true: <em>you cannot mix architectures</em>.  This means that if your apache/mod_php and/or php binary are only i386, then <em>all</em> of the libraries on your system that will be used must contain the i386 architecture.  Likewise, apache/mod_php and/or php binary are only x86_64, then <em>all</em> of your libraries must contain the x86_64 architecture.  Failing to have this, you will get a message like this for example:</p>
<p>[sourcecode language="text"]<br />
PHP Warning:  PHP Startup: Unable to load dynamic library &#8216;/usr/local/zend/lib/php_extensions/gearman.so&#8217; &#8211; dlopen(/usr/local/zend/lib/php_extensions/gearman.so, 9): no suitable image found.  Did find:<br />
/usr/local/zend/lib/php_extensions/gearman.so: mach-o, but wrong architecture in Unknown on line 0<br />
[/sourcecode] </p>
<p>Now that we understand that executables and libraries can have multiple architectures, let&#8217;s get to the task at hand: making sure new extensions can run with <a href="http://www.zend.com/en/products/server-ce/">Zend Server CE</a>.</p>
<p>Zend Server CE for Mac (as of this writing), comes compiled as an i386 executable only.  This includes the PHP binary, php library, and apache binaries that come shipped with ZSCE.  While ZSCE works great out the box with all the provided extensions, you might find that you want some additional 3rd party PHP extensions compiled/linked into this stack.  That&#8217;s where things get a little confusing, and in this post, we&#8217;ll look at how to install the <a href="http://pecl.php.net/package/gearman">gearman extension</a>.</p>
<p>PHP Extensions are basically wrappers around existing libraries, so generally, these extensions require the base library to already be on the system.  In our case, we need &#8220;libgearman&#8221; compiled and on our system for us to be able to compile and use the PHP Gearman Extension.</p>
<p>At this point, I would generally instruct you to compile Gearman with multiple architectures and install (&#8211;prefix=/usr/local).  (Note: to compile for multiple architectures, simply do the following):</p>
<p>[sourcecode language="text" highlight="1"]<br />
    export CFLAGS=&#8217;-arch i386 -arch x86_64&#8242;<br />
[/sourcecode] </p>
<p>In the particular case of Gearman, this will not work as the Gearman makefile utilizes flags that are not compatible with multiple architecture targets.  As such, we go to plan B.</p>
<p>Plan B is something I generally do to keep my system clean: statically building libraries.  I have a personal rule of not keeping i386 only libraries installed in common places like /usr/lib or /usr/local/lib, in this case /usr/local/lib/libgearman.dylib.  Since this is the case, I&#8217;ll build Gearman statically, compile it into the PHP Gearman Extension, and this will allow me to remove the temporary Gearman installation which will have to be i386 only.</p>
<p>[sourcecode language="text" highlight="4,11,12,13,17,21,23,26,27,28,34,35,36,37,42,43,44,50,56"]<br />
    # check to ensure we have a multi-arch libevent (if not go create it as<br />
    # normal with CFLAGS=&quot;-arch i386 -arch x86_64&quot; and install to /usr/local)</p>
<p>    /usr/local/src/gearmand-0.13# file /usr/local/lib/libevent.dylib<br />
        /usr/local/lib/libevent.dylib: Mach-O universal binary with 2 architectures<br />
        /usr/local/lib/libevent.dylib (for architecture i386):  Mach-O dynamically linked shared library i386<br />
        /usr/local/lib/libevent.dylib (for architecture x86_64):        Mach-O 64-bit dynamically linked shared library x86_64</p>
<p>    # next compile gearman to a temp location</p>
<p>    /usr/local/src/gearmand-0.13# export &quot;CFLAGS=-arch i386&quot;<br />
    /usr/local/src/gearmand-0.13# ./configure &#8211;disable-shared &#8211;prefix=/usr/local/gearman-tmp<br />
    /usr/local/src/gearmand-0.13# make &amp;&amp; make install<br />
        [gearman installed now, this should only have static files]</p>
<p>    # ensure we only have a .a library file for gearman<br />
    /usr/local/src/gearmand-0.13# ls /usr/local/gearman-tmp/lib/<br />
        libgearman.a    libgearman.la   pkgconfig</p>
<p>    # make sure zend/bin is first on your PATH<br />
    /usr/local/zend/tmp# echo $PATH<br />
        /usr/local/zend/bin:/var/root/.bin:/usr/local/git/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin<br />
    /usr/local/zend/tmp# which phpize<br />
        /usr/local/zend/bin/phpize</p>
<p>    # next, go to our zend server location, and pull down gearman extension<br />
    /usr/local/src/gearmand-0.13# cd /usr/local/zend/tmp/<br />
    /usr/local/zend/tmp# pecl download gearman-beta<br />
        downloading gearman-0.7.0.tgz &#8230;<br />
        Starting to download gearman-0.7.0.tgz (29,258 bytes)<br />
        &#8230;&#8230;&#8230;done: 29,258 bytes<br />
    File /usr/local/zend/tmp/gearman-0.7.0.tgz downloaded</p>
<p>    # next, unpack, phpize, and statically compile<br />
    /usr/local/zend/tmp# tar zxf gearman-0.7.0.tgz<br />
    /usr/local/zend/tmp# cd gearman-0.7.0<br />
    /usr/local/zend/tmp/gearman-0.7.0# phpize<br />
        Configuring for:<br />
        PHP Api Version:         20090626<br />
        Zend Module Api No:      20090626<br />
        Zend Extension Api No:   220090626<br />
    /usr/local/zend/tmp/gearman-0.7.0# ./configure &#8211;with-gearman=/usr/local/gearman-tmp/ &#8211;disable-shared<br />
    /usr/local/zend/tmp/gearman-0.7.0# make<br />
    /usr/local/zend/tmp/gearman-0.7.0# make install<br />
        Installing shared extensions:     /usr/local/zend/lib/php_extensions/</p>
<p>    # Now go add extension=gearman.so to your php.ini file inside /usr/local/zend/etc/php.ini</p>
<p>    # Now go check that php will have gearman support<br />
    /usr/local/zend# php -i | grep gearman<br />
        gearman<br />
        gearman support =&gt; enabled<br />
        libgearman version =&gt; 0.13</p>
<p>    # Since we statically compiled it, we can remove our temp install of gearman<br />
    /usr/local/zend# rm -Rf /usr/local/gearman-tmp/<br />
[/sourcecode] </p>
<p>At this point, you now have a 3rd party PECL extension that is compiled and working with ZSCE on Mac OS.X.</p>
<div class="shr-publisher-98"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2010/05/12/compiling-gearman-or-anything-for-zend-server-ce-on-snow-leopard/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHPundamentals Series: A Background on Statics (Part 1 on Statics)</title>
		<link>http://ralphschindler.com/2010/05/06/phpundamentals-series-a-background-on-statics-part-1-on-statics</link>
		<comments>http://ralphschindler.com/2010/05/06/phpundamentals-series-a-background-on-statics-part-1-on-statics#comments</comments>
		<pubDate>Thu, 06 May 2010 14:59:55 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[PHPundamentals]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=86</guid>
		<description><![CDATA[Just beyond reading the title, you&#8217;ve more than likely come to this article as the curious yet uninformed, the mad and raving lunatic, or as an enlightened one. Static class members (from here on called simply, &#8220;statics&#8221;) in PHP conjure both the best and worst in developers for a variety of reasons. In part 1 [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Just beyond reading the title, you&#8217;ve more than likely come to this article as the <em>curious yet uninformed</em>, the <em>mad and raving lunatic</em>, or as <em>an enlightened one</em>.  Static class members (from here on called simply, &#8220;statics&#8221;) in PHP conjure both the best and worst in developers for a variety of reasons.  In part 1 of this series of articles on statics, we&#8217;ll explore some background to get a better understanding of statics in PHP.</p>
<h4>Some <em>Static</em> Background And Understanding</h4>
<p>Before we can move into the arguments that surround statics, we first need to understand what they are in the context of PHP.  The core of the PHP language and runtime can draw some pretty big corollaries from the Java/JVM and C#/.NET language platforms.  The biggest, and most important for the purposes of this article, is PHP&#8217;s object model.  Like Java and .NET, PHP follows a class-based, single-inheritance, multiple-interface model- a tenet described by the grandfather of OO languages: <a title="Smalltalk" href="http://en.wikipedia.org/wiki/Smalltalk">smalltalk</a>.  Of course, PHP applies its own &#8220;perspective&#8221; when it comes to the actual implementation details in that of typing, casting, mixed-paradigm usage, and so on; but the foundation for the object model is clearly defined.</p>
<p>That said, it is easy for the PHP community to draw comparisons and, more importantly, &#8220;borrow&#8221; best practices from both the Java and .NET communities.  We certainly have borrowed our fair share with regards to development time tools, infrastructure tools and design patterns.  Over the past 5 to 7 years, there has been an increasing adoption of best practices and patterns from the enterprise Java community, particularly in the form of two major texts: GoF and PoEAA.  The GoF (<a title="Gang Of Four / Design Patterns" href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1273084312&amp;sr=1-1">Gang of Four</a>) text primarily discusses best practices in the form of code structure and reuse: factory, singleton, adapter, composite, facade, iterator and observer to name a few.  PoEAA (<a title="Patterns of Enterprise Application Architecture" href="http://www.amazon.com/exec/obidos/ASIN/0321127420">Patterns of Enterprise Application Architecture</a>), on the other hand, attempts to solve higher order problems, particularly architectural problems at the application layer: MVC, Page Controller, Front Controller, Domain Model, Table and Row Gateway, and so on.  While the examples are primarily executed in Java, they are structurally similar when implemented in PHP, so much so that PHP developers can read the Java examples as pseudo-code.  This is what makes these patterns so applicable and thus popular in the PHP community.</p>
<p>Since we now know where these usage patterns originated, we should have a look at the target language platform: PHP.  The key concept which delineates the PHP platform from the JVM and .NET platforms, is that PHP by default assumes a <a title="Shared-Nothing Architecture" href="http://en.wikipedia.org/wiki/Shared_nothing_architecture">shared-nothing architecture</a>.  What does this mean?  It means out of the box, PHP is not a persistent application platform.  PHP&#8217;s runtime is built around the notion of primarily solving the web problem.  In turn, since the web is request driven, you might say that an application written in PHP is also request driven.  Put another way, the scope of your application is bound to a single request.  The shared-nothing aspect means that the state of the application is built-up and torn-down upon the start and completion of each request to your application.  Conversely, Java and .NET offer a persistent application stack which means the application&#8217;s state exists separate from the requests that come in via the web server.  So, in PHP, the many requests each contain a single running instance of your application.  In Java/.NET, the single application running handles the many requests.</p>
<h4>Statics in Analogies</h4>
<p>Still don&#8217;t get it? Let&#8217;s talk in a couple of analogies.  Let&#8217;s assume we&#8217;ve built a basic application with the &#8220;out-of-the-box&#8221; technologies offered; one built on top of PHP and the other built on top of Java (or .NET, you can choose.)  With your Java/.NET application, if a request is never received from your web server, the application <strong>is</strong> indeed still running.  In PHP, if a request is never received from your web server, the application has NEVER run.  The runtime of a Java/.NET application might be hours or days, whereas the runtime of a PHP application is a long as it takes to service the request.  This analogy&#8217;s mileage may vary, and it is surely intended for demonstrative purposes.  You could inject any number of monkey wrenches into it, but for all intents and purposes- it&#8217;s correct and it works.</p>
<p>Understanding the full scope of an applications runtime state is the most important aspect into understanding the role of static class members in OO programming.  Static class members live as long as the application runtime is valid and alive.  What this means it is that any class member state that has been set during any operation during the applications runtime will persist until the application ceases to exist.  Looking back at our main platform differences, we can see that in the Java/.NET platform, statics members created in the scope of an application layer will be around until someone pushes the &#8220;shutdown&#8221; button on that application.  This could mean a static member or static state is persisted for hours, days, or even longer.  Like these persistent application stacks, PHP will destroy any static members and state at the end of the applications lifecycle.  Unlike these persistent application stacks, the application lifecycle ends with the completion of a web request.  This means that static members and static state in PHP, for the average web application, sticks around for seconds or less and is only valid in the context of a single web request.</p>
<h4>Statics in Pictures</h4>
<p><em>Still</em> don&#8217;t get it?  Lets have a look at a few images to better explain these concepts.</p>
<p>The following images will attempt to explain the various layers of a web application, one from the perspective of the JVM/.NET platform, the other from the perspective of the PHP platform. (For all intents and purposes, the PHP platform could also be any scripting language executed by an apache module or fastcgi.)</p>
<p>The <span style="color: #339966;">green</span> layer is the web server layer, this is the process that will attach to port 80 and listen for requests.  The <span style="color: #0000ff;">blue</span> layer represents the application process itself.  This layer is responsible for global application state and class-based static state.  The <span style="color: #ff6600;">orange</span> layer is a request which comes in from the web, this is typically what we&#8217;ve called a page request.  Inside of each web request is the <span style="color: #ffff00;">yellow</span> layer, which represents the page-lifecycle.  In terms of the application, this is where all of the request specific application routines happen including page startup and business logic.</p>
<p><a href="http://ralphschindler.com/wordpress/wp-content/uploads/2010/05/Java-and-.NET-Application-Architecture.png"><img class="aligncenter size-full wp-image-90" title="Java and .NET Application Architecture" src="http://ralphschindler.com/wordpress/wp-content/uploads/2010/05/Java-and-.NET-Application-Architecture.png" border="0" alt="" width="596" height="363" /></a></p>
<p>Contrasted against &#8230;</p>
<p><a href="http://ralphschindler.com/wordpress/wp-content/uploads/2010/05/PHP-Application-Architecture.png"><img class="aligncenter size-full wp-image-89" title="PHP Application Architecture" src="http://ralphschindler.com/wordpress/wp-content/uploads/2010/05/PHP-Application-Architecture.png" border="0" alt="" width="596" height="404" /></a></p>
<p>The most important thing to take away from these images, particularly with respect to understanding statics, is the <span style="color: #0000ff;">blue</span> layer, or the layer that best represents the scope of globals and static members.  This is the heart of what is meant by a &#8220;shared-nothing&#8221; architecture.  It is this key difference that affects how we architect the code for our web applications.</p>
<p>In the next article in this series, we&#8217;ll have a look at PHP&#8217;s application architecture in greater detail and how it solves problems that might arise from a shared-nothing style architecture, why this architecture is arguably better for the web and cloud based services, but most importantly, how statics fit into this paradigm.</p>
<div class="shr-publisher-86"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2010/05/06/phpundamentals-series-a-background-on-statics-part-1-on-statics/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>The Anatomy Of A Bug/Issue Reproduction Script</title>
		<link>http://ralphschindler.com/2010/02/18/the-anatomy-of-a-bug-issue-reproduction-script</link>
		<comments>http://ralphschindler.com/2010/02/18/the-anatomy-of-a-bug-issue-reproduction-script#comments</comments>
		<pubDate>Thu, 18 Feb 2010 22:17:14 +0000</pubDate>
		<dc:creator>Ralph Schindler</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Quality Assurance]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://ralphschindler.com/?p=60</guid>
		<description><![CDATA[&#8220;There is a problem with component Fooey-Bar-Bazzy, I think it&#8217;s related to Nanny-Nanny-Neener. Please Fix Now.&#8221; If you&#8217;ve written a bug/issue report like that in the past with no other details- shame on you! This may come as a shock, but as great as some developers might be, they cannot read minds. Each has their [...]]]></description>
				<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>&#8220;There is a problem with component Fooey-Bar-Bazzy, I think it&#8217;s related to Nanny-Nanny-Neener. Please Fix Now.&#8221; If you&#8217;ve written a bug/issue report like that in the past with no other details- shame on you!  This may come as a shock, but as great as some developers might be, they cannot read minds.  Each has their own way of coding, custom working environment as well as their own favorite tools; aside from variances in coding standards and best practices.  Some could argue these little intricacies are outside of the realm of coding standards and best practices and that these are the differences between good, great, and even terrible developers.  Each developer has a different opinion on how particular applications, libraries of code, or even features of a particular project are expected to behave in practice.  These varying expectations are why bugs/issues exist.  No one developer producing code for mass consumption can anticipate every possible use case.  Additionally, no one developer can replicate every environment surrounding every pre-conceived use case.  There are simply not enough resources at hand; be it in the form of a variety of systems or simply the number of hours in a developers day.</p>
<p>With that in mind, I write this as a plea to all developers to be good to the maintainer of code you use.  In the simplest form of advice, I suggest that before you click submit on that bug/issue report form, ask yourself two questions: &#8220;Did I do enough due-diligence in determining if this is really a bug?&#8221; AND &#8220;If <b>I</b> got this bug report, would I be able to reproduce it.. let alone understand it?&#8221;.  If the answer is YES to both of those questions.  Go ahead- click submit.  If your answer is no, you&#8217;ve got some more work to do.</p>
<h3>Some Tenets Of the Good Reproduction Script</h3>
<p>In this short article, I&#8217;d like to outline a few details of what should go into a bug/issue report.  These are some simple guidelines that should be considered when you write a bug/issue report.  It should be noted that this list is by all means not exhaustive, but if you at least consider the list below before clicking submit- you&#8217;ll make a code maintainers day.  I promise. </p>
<ol>
<li><b>List Out All Assumptions Clearly</b>
<p>    PHP specifically is well known for being a &#8220;glue language&#8221;.  What that means is that PHP is generally sitting between multiple pieces of software that is, of course, <i>not PHP</i>.  This means that these pieces of software each have their own set of configurations and environments that PHP is &#8220;gluing&#8221; together.  That being the case, any assumptions <em>about non-PHP assumptions should be clearly listed</em> in the reproduction script.  This could include database flavor and its settings, a PHP library component, or perhaps a specific version of an extension that is being used and the underlying unmanaged/c-based library your PHP environment is consuming.
    </li>
<li><b>Use The Shortest Possible Use Case</b>
<p>    As tempting as it is to copy a script from your project and paste it into the bug/issue submission box, don&#8217;t do this.  If you are truly invested in seeing the bug/issue fixed in a timely fashion, take the time to create a small reproduction script.  In this script should be the <em>absolute minimal amount of code</em> to demonstrate to another human that there is indeed a problem that needs solving. By keeping the script minimal and short, you are also removing any other distractions from the script that otherwise might confuse the maintainer and prevent him from fully understanding the real problem.
    </li>
<li><b>Use Generic Yet Meaningful Names</b>
<p>    It cannot be stressed enough that any non-meaningful names should be discouraged at all costs. And as mentioned above, you want to have as few distractions as possible in the use case.  For example, supplying your database table of customers, with first_name, last_name, etc has virtually nothing to do with the problem at hand.  In these cases where table and column names are ancillary to the actual problem, they should be generalized: a table named &#8216;foo&#8217;, and columns named &#8216;bar1&#8242; and &#8216;bar2&#8242;. Unless &#8230;</p>
<p>    &#8230; the variable name can add context to the problem.  What does this mean? $customer would be bad; but $faultyTableObject is good.  The latter naming makes it easy for the maintainer to focus on the variable that need to be tracked leading up to the problem.
    </li>
<li><b>Document Both What You Expect, And The Actual Result</b>
<p>    Claiming something is broken without offering what you expect and what the actual result is offers next to nothing to the maintainer attempting to fix the problem.  Generally speaking, most use cases that end up being bugs/issues are outside of the original preconceived use cases for the actual component.  That said, the maintainer is going to need the context of the use case that you&#8217;ve found to be problematic.  It also helps to point out any existing documentation that describe the more well-defined uses cases, and how your use case relates and/or deviates from those already defined use cases.
    </li>
<li><b>Make The Reproduction Script As Generic As Possible</b>
<p>    Perhaps this is redundant, but it&#8217;s important to know the minimal requirements for reproducing a bug/issue.  You are not expected to be an expert on how to fix the actual problem, but you should do your own due-diligence in order to hand the problem off to the maintainer.  It&#8217;s already been said to &#8220;List out all assumptions clearly&#8221;, but it is just as important to peel off any specific pieces of the problem that are not directly part of the problem.  </p>
<p>    This concept can best be described by example.  While MySQL is a widely available database platform, SQLite is widely known as the easiest to use and most portable database platform, at least in the PHP runtime.  If you find a problem while using mysql, but it&#8217;s clear it can be replicated using SQLite, use SQLite.  SQLite is built into PHP by default, and in a single script, you can create a memory based database and its schema in just a few lines of code.</p>
<p>    Sometimes a issue cannot be described in a single script. This is ok. This would be the case if, for example, you found an issue in a larger system, like Zend Frameworks MVC layer.  In this case, it makes sense that you need to provide a minimal ZF project to demonstrate the issue.  In these cases, make sure to again, use a few files and as little code as possible to demonstrate the issue.  Also, in the spirit of using generic code, ensure to make <em>all file system paths relative</em>. This will help the maintainer get up and running with the problematic project in a minimal amount of time, with minimal configuration.
    </li>
</ol>
<h3>A Reproduction Script By Example</h3>
<p>The following is a reproduction script I have written based on an issue (<a href="http://framework.zend.com/issues/browse/ZF-3709">ZF-3709</a>) provided to Zend Framework in our issue tracker.  I chose this issue to write a reproduction for because it offers the ability to talk about how one might go about describing the environment, more specifically what the database should look like in order to replicate the problem.</p>
<p><small>(This script can also be found at <a href="http://gist.github.com/307396">http://gist.github.com/307396</a>)</small></p>
<p></p><pre class="crayon-plain-tag">&amp;lt;?php

/**
 * This reproduction script shall accompany the issue reported at 
 * http://framework.zend.com/issues/browse/ZF-3709
 *
 * Assumptions:
 *   Zend_Db_Table_* from trunk
 *   PHP Environment has SQLite with :memory: capabilities
 *
 * Result:
 *   This script should run without any assertions failing (empty output)
 */



// ensure that Zend Framework trunk is being tested against &amp;amp; classes are available
// set_include_path('/path/to/ZendFramework/library');
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

// setup the adapter, this uses SQLite so that its minimally invasive
// to anyone wishing to reproduce the issue on their local machine
$dbAdapter = Zend_Db::factory(
    'Pdo_Sqlite',
    array('dbname' =&amp;gt; ':memory:')
    );

// ensure all tables have access to the adapter
Zend_Db_Table::setDefaultAdapter($dbAdapter);

// setup the database, classes, &amp;amp; assertion system
setup();



/**
 * BEGIN Reproduction Code 
 */



// find a record that has a relationship to some bars through foo_to_bar
$fooTable = new Foo();
$fooRow = $fooTable-&amp;gt;fetchRow('id = 2');
$fooIdOnesBars = $fooRow-&amp;gt;findManyToManyRowset('Bar', 'FooToBar');

// the expected values for the next call
$expectedValues = array(
    array('id' =&amp;gt; '2', 'name' =&amp;gt; 'bravo'),
    array('id' =&amp;gt; '3', 'name' =&amp;gt; 'charlie')
    );


// when we loop through the rows, they should match the expected results above
foreach ($fooIdOnesBars as $index =&amp;gt; $barRow) {
    // I'll use assert here to throw warnings when expected does not match actual
    $actualValue = $barRow-&amp;gt;toArray();
    assert($expectedValues[$index] === $actualValue);
}



/**
 * END Reproduction Code
 *
 * Supporting code below
 */ 


// setup function
function setup() {
    setup_database();
    setup_classes();
    setup_assertions();
}

// This function will setup the proper database structure with test data
function setup_database() {
    global $dbAdapter;

    $conn = $dbAdapter-&amp;gt;getConnection();
    $conn-&amp;gt;query('
        CREATE TABLE foo (
            id INTEGER PRIMARY KEY, 
            name VARCHAR(25)
            );
        ');

    foreach (array('one', 'two', 'three', 'four') as $numberName) {
        $conn-&amp;gt;query('INSERT INTO foo (name) VALUES (&amp;quot;' . $numberName . '&amp;quot;);');
    }

    $conn-&amp;gt;query('
        CREATE TABLE bar (
            id INTEGER PRIMARY KEY,
            name VARCHAR(25));
        ');
            
    foreach (array('alpha', 'bravo', 'charlie', 'delta') as $word) {
        $conn-&amp;gt;query('INSERT INTO bar (name) VALUES (&amp;quot;' . $word . '&amp;quot;);');
    }

    $conn-&amp;gt;query('
        CREATE TABLE foo_to_bar (
            id INTEGER PRIMARY KEY,
            foo_id INTEGER,
            bar_id INTEGER,
            extra VARCHAR(20)
            );
        ');
    $datas = array(
        array('foo_id' =&amp;gt; 2, 'bar_id' =&amp;gt; 2, 'extra' =&amp;gt; 'Two to Two'),
        array('foo_id' =&amp;gt; 2, 'bar_id' =&amp;gt; 3, 'extra' =&amp;gt; 'Two to Three'),
        array('foo_id' =&amp;gt; 3, 'bar_id' =&amp;gt; 4, 'extra' =&amp;gt; 'Three to Four'),
        );
    foreach ($datas as $datum) {
        $conn-&amp;gt;query('INSERT INTO foo_to_bar '
            . '(' . implode(',', array_keys($datum)) . ')'
            . ' VALUES (&amp;quot;' . implode('&amp;quot;, &amp;quot;', array_values($datum)) 
            . '&amp;quot;);');
    }
}

// This function will define the proper Zend_Db_Tables and their relationships
function setup_classes() {
    
    class Foo extends Zend_Db_Table_Abstract
    {
        protected $_name = 'foo';
    }
    
    class Bar extends Zend_Db_Table_Abstract
    {
        protected $_name = 'bar';
    }
    
    class FooToBar extends Zend_Db_Table_Abstract
    {
        protected $_name = 'foo_to_bar';
        protected $_referenceMap = array(
            'Foo' =&amp;gt; array(
                'columns' =&amp;gt; 'foo_id',
                'refTableClass' =&amp;gt; 'Foo',
                'refColumn' =&amp;gt; 'id'
                ),
            'Bar' =&amp;gt; array(
                'columns' =&amp;gt; 'bar_id',
                'refTableClass' =&amp;gt; 'Bar',
                'refColumn' =&amp;gt; 'id'
                )
            );
    }
    
}

// assertion setup
function setup_assertions() {
    assert_options(ASSERT_ACTIVE, true);
    assert_options(ASSERT_WARNING, false);
    assert_options(ASSERT_CALLBACK, 'assert_failure');
}

// callback for assertion failures
function assert_failure() {
    global $expectedValues, $index, $actualValue;
    echo 'Was expecting an array that looked like:' . PHP_EOL;
    var_dump($expectedValues[$index]);
    echo 'But got array that looked like:' . PHP_EOL;
    var_dump($actualValue);
    echo PHP_EOL . PHP_EOL;
}</pre><p></p>
<p>To the best of my ability, this script passes both of my earlier questions: &#8220;Yes, I did enough due-diligence in determining if this is really a bug.&#8221; AND &#8220;Yes, if I got this bug report, would I be able to reproduce it and understand it.&#8221;</p>
<h3>A Few Considerations</h3>
<p>This above script does not have unit tests, nor does it represent a patch to the existing framework.  While that would be the most ideal, that sets the bar much too high for people to report worthwhile issues.  The consumers of the code are not expected to be experts on the actual issue at hand, or even how to write valid unit tests that fully exercise a feature or bug.  Ultimately, as a code maintainer, I simply want to be able to see the issue you are attempting to describe.</p>
<p>If you&#8217;d like to go above and beyond the standard reproduction script, you might also considering offering lines of code that you feel might be problematic.  What that allows is maintainers to set breakpoints at specific locations and really drill down into the offending code.</p>
<p>I hope this helps developers understand what is expected of them as they file issue reports on open source code they use.  By following these guidelines you&#8217;ll be doing a service to the maintainer by making their life easier, and even your own since reproduction scripts offer quicker turn around time for issues over those that require in-depth research.</p>
<div class="shr-publisher-60"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://ralphschindler.com/2010/02/18/the-anatomy-of-a-bug-issue-reproduction-script/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

 Served from: ralphschindler.com @ 2013-06-20 04:02:37 by W3 Total Cache -->