<?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>Will Fitch&#039;s Blog &#187; PHP</title>
	<atom:link href="http://www.willfitch.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using ActiveRecord in Fever Framework</title>
		<link>http://www.willfitch.com/using-activerecord-in-fever-framework.html</link>
		<comments>http://www.willfitch.com/using-activerecord-in-fever-framework.html#comments</comments>
		<pubDate>Tue, 16 Feb 2010 13:46:29 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[lamp]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=234</guid>
		<description><![CDATA[One of the features of the PHP Fever Framework is ActiveRecord access to your database. This is meant to be a database access tool for DB models and contains many of the ActiveRecord functions and more.]]></description>
			<content:encoded><![CDATA[<p>One of the features of the PHP Fever Framework is ActiveRecord access to your database. This is meant to be a database access tool for DB models and contains many of the <a href="http://en.wikipedia.org/wiki/Active_record_pattern">ActiveRecord</a> functions and more.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/using-activerecord-in-fever-framework.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fever Framework Update</title>
		<link>http://www.willfitch.com/fever-framework-update.html</link>
		<comments>http://www.willfitch.com/fever-framework-update.html#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:06:35 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PHP Fever Framework]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=224</guid>
		<description><![CDATA[I just want to update everyone on the status of the framework. It is still on schedule for the February 26th release! As we countdown to the public beta release, I want to keep you informed of some exciting functionality that&#8217;s being added. Functionality Along with the JSON controller, a new XML controller has been [...]]]></description>
			<content:encoded><![CDATA[<p>I just want to update everyone on the status of the framework. It is still on schedule for the February 26th release!</p>
<p>As we countdown to the public beta release, I want to keep you informed of some exciting functionality that&#8217;s being added.</p>
<p><strong>Functionality</strong></p>
<p>Along with the JSON controller, a new XML controller has been added. This allows you to return nearly anything from an action and it will automatically convert it to XML, send the proper content-type header and provide the data to the client.  Here is an example of how it would work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyController <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Controller\Action\AbstractXml
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTestData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span>
            <span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Will Fitch'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">76</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'hair'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'brown'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'eyes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'brown'</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will, in turn, produce the following XML document to the client:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;results<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Will Fitch<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;height<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>76<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/height<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hair<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>brown<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hair<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eyes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>brown<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eyes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/attributes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/results<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now I&#8217;m sure there are those out there that are screaming, &#8220;But I want to customize the name of the elements!!!&#8221;. No problem!  There is a method withing the AbstractXml controller called &#8220;setXmlElement&#8221; which accepts an instance of SimpleXMLElement. This will be used when converting data provided into XML.</p>
<p>This is all thanks to a new component called &#8220;XmlConverter&#8221; which lives in the \Fever\IO\Xml namespace. It follows the <a href="/the-chain-of-command-pattern-oop-techniques-in-php.html">chain-of-command</a> pattern and iterates until it finds the correct data type provided. Here are a list of converters available for iteration:</p>
<ul>
<li>ArrayToXml</li>
<li>BooleanToXml</li>
<li>NullToXml</li>
<li>NumberToXml</li>
<li>ObjectToXml</li>
<li>StringToXml</li>
</ul>
<p>All of these are separate entities, so they can be used independently.</p>
<p>I&#8217;ll continue to update throughout the upcoming days before the public beta!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/fever-framework-update.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing the PHP Fever Framework</title>
		<link>http://www.willfitch.com/introducing-the-php-fever-framework.html</link>
		<comments>http://www.willfitch.com/introducing-the-php-fever-framework.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 16:48:07 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP Fever Framework]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=190</guid>
		<description><![CDATA[Overview The birth of a new MVC framework, like most others, is spawned by frustrations with the functionality of one or more other frameworks: too much emphasis on design theory, not enough in others, speed, flexibility&#8230; the list goes on. The Fever Framework is no different. In an attempt to find a happy medium with [...]]]></description>
			<content:encoded><![CDATA[<h3>Overview</h3>
<p>The birth of a new MVC framework, like most others, is spawned by frustrations with the functionality of one or more other frameworks: too much emphasis on design theory, not enough in others, speed, flexibility&#8230; the list goes on.  The Fever Framework is no different.  In an attempt to find a happy medium with design theory, speed, flexibility and latest PHP functionality, this framework is targeted at many audiences from small, shared-hosting environments to enterprise-level web applications.  That said, let me provide some goals I&#8217;ve kept in mind while developing the Fever Framework.</p>
<h3>Some Goals</h3>
<p><strong>Design theory and flexibility</strong></p>
<p>I have often wrestled among object-oriented design theory, development time and speed, not to mention optimization which is usually the 800 pound gorilla in the room.  Developers deal with this all the time; trying to squeeze a few more days in to ensure the code looks as good as it behaves.</p>
<p>The Fever Framework implements many OOP design theories and MVC functions.  However, in some cases, it specifically goes out of the way to provide an easier interface for developers.  For example, let&#8217;s say you&#8217;re implementing a series of actions which return JSON encoded objects.  In many frameworks, this is taken care of within the model, provided back to the controller and sent to the browser.  In the Fever Framework, you have a few options:</p>
<p><em>Controller-level</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> MyBlog <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Controller\Json
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPost<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BlogPosts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above example is a controller which extends the Json controller.  The &#8220;BlogPosts&#8221; class is a model which extends the \Fever\Db\ActiveRecord class.  The &#8220;findByPrimaryKey&#8221; is a method provided within AR.  Once the object is returned, the Json controller handles the JSON encoding and sending proper HTTP headers to the client.</p>
<p><em>Model-level</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> BlogPosts <span style="color: #000000; font-weight: bold;">extends</span> \Fever\Db\ActiveRecord
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getBlogPost<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">findByPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</span> <span style="color: #339933;">=&gt;</span> \MyApp\Db<span style="color: #339933;">::</span><span style="color: #004000;">getReadInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This example returns the data already JSON encoded to the caller.  It extends the ActiveRecord class, set&#8217;s the data via &#8220;setData&#8221; (provide in DataModel), and chains &#8220;toJson&#8221; to the request.  This returns the JSON encoded string needed.</p>
<p>This is only two of many ways to achieve the above goal.  Design theory and flexibility aren&#8217;t mutually exclusive, and flexibility is just as important as design.</p>
<p><strong>Latest PHP  functionality</strong></p>
<p>As you&#8217;ve already noticed, I&#8217;ve incorporated namespaces into the framework.  This is especially nice when editing inside an IDE and you can dive down into each namespace to find code (remember the frameworks that use hack namespace styles that make the class name 50+ characters long?).  The naming convention is easy to read and makes sense!</p>
<p><strong>Speed</strong></p>
<p>The Fever Framework can be used as a set of libraries, but I&#8217;ve taken the liberty to assume you&#8217;ll be using autoloading.  There is a provided class, Fever\Autoloader which takes care of the inclusion of files for classes/namespaces.  Since FF is setup to so that each class falls under a directory based on it&#8217;s namespace, autoloading is fast and efficient!  With that in mind, there is an important feature (some might argue) you should keep in mind.</p>
<p>Each class comes with a comment block that contains all of the file dependencies as require_once statements.  If you so choose, you can remove the comment block and everything will be just fine.  However, keep in mind that each require_once call makes a full file scan.  This is extremely unoptimized!</p>
<p>When you get the code base , you&#8217;ll also notice that a printed stack trace will be relatively short.  You won&#8217;t have 30 classes printed out that are abstracts of abstracts of abstracts of interfaces.  Like I said before, design theory isn&#8217;t the most important feature!</p>
<p><strong>Models</strong></p>
<p>As a PHP developer, this is likely where you&#8217;ll spend most of your time.  Like Zend Framework, Fever doesn&#8217;t come with a abstract model.  It provides a subset of classes for you to decide to use.  For example, if you&#8217;re working at the table level, you might choose to use Fever\Db\ActiveRecord, which provides most of the functionality of the Active Record pattern.  If you are only using stored procedures, Fever\Db\Procedure might be your choice.  But databases aren&#8217;t the only types of models.  What if you&#8217;re working with a SOAP client?  You could extend the Fever\Soap\Client, which provides functionality related to SOAP.</p>
<p><strong>Modules</strong></p>
<p>Module-based application development is included.  If you choose to use this, you simply add a couple of lines of code to your global bootstrap.  If not, do nothing!</p>
<p>Each module is allowed to have its own bootstrap.  That means you can customize it as a separate application.  You can also define how modules are recognized.  If each module is a subdomain, you simply provide that match to the router, and it will recognize it.  If it&#8217;s a subdirectory, do the same!</p>
<p><strong>Views</strong></p>
<p>To provide some options, the framework comes with both a standard PHP view type as well as Smarty.  By default, the standard PHP type is used.  This is easily changed within the bootstrap of your application, or even module.  Each view type comes with a set of helpers as well.</p>
<h3>How do I get the code?</h3>
<p>The alpha review will start on February 19th.  This will include developer code reviews and testing.  This circle will be relatively small (10 &#8211; 20 developers) and will last one week.  The public beta will begin on February 26th, and a download will be available here.</p>
<p>If you are interested in joining the alpha review, please email me at will[at]phpfever[dot]com or <a href="/contact-me">use the contact form</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/introducing-the-php-fever-framework.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>April Nashville PHP Users Group Meeting</title>
		<link>http://www.willfitch.com/april-nashville-php-users-group-meeting.html</link>
		<comments>http://www.willfitch.com/april-nashville-php-users-group-meeting.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:45:37 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[nashville php users group]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=167</guid>
		<description><![CDATA[The April 2009 users group meeting will focus on the Symfony framework.  We will be receiving a presentation from <a href="http://www.centresource.com">Centre{source}</a> where one of the developers is on the Symfony development team.  More details to come later.]]></description>
			<content:encoded><![CDATA[<p>The April 2009 users group meeting will focus on the Symfony framework.  We will be receiving a presentation from <a href="http://www.centresource.com">Centre{source}</a> where one of the developers is on the Symfony development team.  More details to come later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/april-nashville-php-users-group-meeting.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Chain-of-Command Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Tue, 10 Mar 2009 12:07:43 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[chain of command]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=160</guid>
		<description><![CDATA[The chain-of-command pattern, like most others, assists with maintaining a loose coupling within your classes.  By providing a series of classes that implement the ICommand interface and do a specific bit of processing, the developer doesn't have to care which method to execute.]]></description>
			<content:encoded><![CDATA[<p>The chain-of-command pattern, like most others, assists with maintaining a loose coupling within your classes.  By providing a series of classes that implement the ICommand interface and do a specific bit of processing, the developer doesn&#8217;t have to care which method to execute.</p>
<p><strong>The ICommand Interface</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> ICommand
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Run a command
     * @param string $command_type
     * @param args $args
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This simple interface provides a method for executing a call to initiate a bit of functionality. Along with the classes that execute the functionality, you&#8217;ll need a class to handle the chain.  We&#8217;ll provide this class with another interface requiring the use of a method to add chains and execute commands:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> ICommandBuilder
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Run a command
	 *
	 * @param string $command_type
	 * @param mixed $args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> runCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Add a new command class
	 *
	 * @param mixed $commanding_class
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$commanding_class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The two methods here are used for executing commands and adding new command classes.  Let&#8217;s build the chain-of-command class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ChainBuilder implements ICommandBuilder
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_commands</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> runCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands <span style="color: #b1b100;">as</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onCommandCall</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$command_type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addCommand<span style="color: #009900;">&#40;</span><span style="color: #000088;">$commanding_class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_commands<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$commanding_class</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class above provides a mechanism for chaining your requests.  You add the necessary commands to execute, then pass requests to runCommand, which will loop through all of the commands until one successfully completes the action, or it exhausts all commands.  Here you could add interface checks and throw the necessary exceptions for commands not found, etc.</p>
<p>Let&#8217;s add a couple of command implementations: email and stream.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> EmailCommand implements ICommand
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// send an email with $args</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> StreamCommand implements ICommand
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCommandCall<span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$command</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'writeStream'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Write the stream</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>These commands email a user and write to a stream.  Now let&#8217;s use them!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$command</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ChainBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCommand</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StreamCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCommand</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EmailCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">runCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'me@you.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">runCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'writeStream'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Hi, stream!'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>At first glance, the chain-of-command pattern may look like overhead.  But on a large code base, this code be useful when making a lot of modifications, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-chain-of-command-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Observer Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Mon, 09 Mar 2009 20:23:04 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=149</guid>
		<description><![CDATA[The observer pattern provides another way to maintain loose coupling within your code.  It's an extremely simple pattern and is implemented similarly across languages.  There are two parts: the observer and the observable object.  Let's address them both starting with the observer.]]></description>
			<content:encoded><![CDATA[<p>The observer pattern provides another way to maintain loose coupling within your code.  It&#8217;s an extremely simple pattern and is implemented similarly across languages.  There are two parts: the observer and the observable object.  Let&#8217;s address them both starting with the observer.</p>
<p><strong>The Observer</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> IObserver
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Method called on event change
	 *
	 * @param mixed $sender
	 * @param mixed $args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onChange<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see from above, the observer implements a single method called onChange.  This method is executed when the observable class changes.  The first parameter is typically the instance of the class changing and the second arguments relative to the change.  Now let&#8217;s look at the IObservable interface:</p>
<p><strong>The Observable Object</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> IObserverable
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Add an observer
	 *
	 * @param mixed $obj
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addObserver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The observable class provides a method for one or more observers to be notified.  The observable class needs to maintain a list of these observers and notify them when necessary.  A common usage of this is a logging mechanism that needs to be notified when data is created, updated and deleted.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UserLogging implements IObserver
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onChange<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Log the changes (we echo here)</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'The class is '</span><span style="color: #339933;">.</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sender</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' and the arguments are '</span><span style="color: #339933;">.</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User implements IObserverable
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_observers</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> notifyObservers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_observers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			 <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_observers<span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">onChange</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notifyObservers</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addObserver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Add logic for interface implementation check here if you want.</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_observers<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addObserver</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UserLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteUser</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">55</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Not only does this provide you some pseudo event access, but this is a much cleaner, maintainable implementation of observing a particular object.</p>
<p><strong>Conclusion</strong></p>
<p>I hope you find ways at work to implement this use pattern.  We more often than not get caught up with getting code written (it comes with the territory) and lose sight of these power and proven designs to get the job done.  I&#8217;m certainly just as much to blame as the next developer! Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-observer-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Iterator Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-iterator-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-iterator-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Wed, 04 Mar 2009 21:21:28 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=137</guid>
		<description><![CDATA[The iterator pattern is one of the most useful, yet unused patterns defined.  It provides a way for class users to count and iterate over a set of objects related to the class.  This is very useful in MVC (Model-View-Controller) models as they handle data and the logic that pertains to it.]]></description>
			<content:encoded><![CDATA[<p>The iterator pattern is one of the most useful, yet unused patterns defined.  It provides a way for class users to count and iterate over a set of objects related to the class.  This is very useful in MVC (Model-View-Controller) models as they handle data and the logic that pertains to it.</p>
<p>PHP provides two interfaces that already define what you should include in an iterator pattern: <a href="http://us3.php.net/iterator">Iterator</a> and <a href="http://us3.php.net/countable">Countable</a>.  Below are their definitions:</p>
<p><strong>Iterator Interface</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> Iterator implements Traversable
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> mixed <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> scalar <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> void <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> void <span style="color: #990000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> bool valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Countable Interface</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">interface</span> Countable
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> int <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You can already iterate over class properties using the foreach() loop, so the Iterator and Countable interfaces are domain specific &#8212; meaning the iteration logic is specific to the functionality of the class.</p>
<p>As an example, let&#8217;s assume we have a domain model that manages users (CRUD).  We have an additional class that acts as a structure (you don&#8217;t have to do this, but it&#8217;s easier to read in a blog) for each individual user.  You also have a method that retrieves an arbitrary number of users, assigns each to a User class (acting as a struct), and gives back an array.  While this is certainly doable, the iterator pattern defines an easier way to process the data.</p>
<p><strong>Example Before Iterator Pattern</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$address</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$city</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$state</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$zip</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User_Management
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * List of users
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$users</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get a user and assign it an instance of User</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAllUsers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get all users, assign each to User class, and add to the self::users array</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The above example is pretty typical for domain models without the use of iterator.  Adding the iterator, we provide an interface to bi-directionally traverse, get key information and count records.</p>
<p><strong>With the Iterator Pattern Implemented</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$address</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$city</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$state</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$zip</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User_Management implements Iterator<span style="color: #339933;">,</span> Countable
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * List of users
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$users</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Position of the iterator
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$position</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Retrieve the current record
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">position</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Return the current key index
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">position</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Increment the iterator index
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">position</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Reset the position
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">position</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get a user and assign it an instance of User</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Validate whether a record at current
	 * position exists
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">position</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Return the total number of users
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAllUsers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get all users, assign each to User class, and add to the self::users array</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The domain model now implements Countable and Iterator.  The below example shows a couple of uses for this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User_Management<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// The $index will contain the current key</span>
	<span style="color: #666666; font-style: italic;">// while the $value will contain the user</span>
	<span style="color: #666666; font-style: italic;">// at the current position</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">address</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Rewind the iterator</span>
<span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">address</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Rewind the iterator</span>
<span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">address</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Conclusion</strong><br />
This is a much cleaner, easier approach to iterating over objects.  I hope you find this usable in your domain models!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-iterator-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Factory Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-factory-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-factory-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Mon, 02 Mar 2009 21:50:07 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=133</guid>
		<description><![CDATA[The purpose of the factory pattern is to assist with maintaining loose coupling.  Code that is tightly coupled is error prone in that if a class is changed, it can have a domino affect to other scripts using it.  This is typical to large-scale systems and smaller systems that grow very fast.]]></description>
			<content:encoded><![CDATA[<p>The purpose of the factory pattern is to assist with maintaining loose coupling.  Code that is tightly coupled is error prone in that if a class is changed, it can have a domino affect to other scripts using it.  This is typical to large-scale systems and smaller systems that grow very fast.</p>
<p>For example, let&#8217;s assume you have a relatively small system that contains a class called Users.  This class reads user data from a small file located on disk.  Your system booms, and you need to move to a RDBMS (Relational Database Management System).  Because of this, your methods in the class, data source and other items change, breaking the code that is utilizing the Users class.  This is where the factory pattern comes in.</p>
<p>The factory pattern&#8217;s purpose is to contain methods that create objects for you.  Instead of directly instantiating the Users class, you request an instance from a factory class (we&#8217;ll call it UsersFactory), and call a static method (we&#8217;ll call it getInstance).</p>
<p><strong>Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Users
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_userId</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserById<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get a user</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_userId <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UsersFactory
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Users<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> UsersFactory<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example above demonstrates how a factory method would work.  Now, let&#8217;s add in that new class that gets the users from the database.  Let&#8217;s assume you have 25 other scripts that use the existing class.  You can create the new Users_From_DB class and create a static method to reference that as well.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Users
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_userId</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserById<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// go get a user</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_userId <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Users_From_DB
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// same class only from db</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UsersFactory
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Users<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getUsersFromDB<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Users_From_DB<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This allows you to maintain a loose coupling with a class that could create major problems in the event it goes through major changes.</p>
<p><strong>Conclusion</strong></p>
<p>I have seen where some use the factory pattern as a singleton.  They look similar but serve two very different purposes.  Take the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Users_Model
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUserById<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// do something</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Model
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$instances</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> factory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model_name</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model_name</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instances</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instances</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$model_name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$instances</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$model_name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$model_name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> Model<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Users_Model'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$model2</span> <span style="color: #339933;">=</span> Model<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Users_Model'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>While the case could be made that it is acting as a factory pattern, it is being used more as a singleton, maintaining a single instance of a class.</p>
<p>The factory pattern does a really good job, and like the singleton, it serves a very specific purpose.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-factory-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Singleton Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Mon, 02 Mar 2009 20:22:55 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=124</guid>
		<description><![CDATA[The singleton pattern is a common pattern used to make resources exclusive in that there is one of a particular type of resource.  The most common usage of this is database connectivity.  Typically, an application only wants a single connection to a single database server at any given time.  This is where the singleton pattern comes in.]]></description>
			<content:encoded><![CDATA[<p>The singleton pattern is a common pattern used to make resources exclusive in that there is one of a particular type of resource.  The most common usage of this is database connectivity.  Typically, an application only wants a single connection to a single database server at any given time.  Another use could be restricting the instance to a specific number of objects.  This is where the singleton pattern comes in.</p>
<p>The singleton pattern is defined as a design pattern that is used to restrict instantiation of a class to one object (or limit number of instances).  This is a pretty straight forward concept and works well with many other OOP techniques including the factory pattern.  The easiest implementation is to create a public static method within that class that instantiates a new instance.</p>
<p><strong>Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> DB_Connection
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        static <span style="color: #000088;">$instance</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DB_Connection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #666666; font-style: italic;">// DO SOME CODE TO RUN A QUERY HERE</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_con<span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'connection_string'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You&#8217;re probably wondering why the class constructor is private.  This seems odd as it&#8217;s impossible to create an object from outside the class, right?  That&#8217;s the point.  If we don&#8217;t limit the class instantiation, the singleton pattern is useless.  The method getInstance() can create an object as it&#8217;s a member of that class.</p>
<p><strong>How does the static method work?</strong></p>
<p>The static method getInstance() declares a static variable called instance ($instance).  The first time this method is called, the $instance variable&#8217;s value is null.  We then check if the value is null &#8212; if it is, we create a new instance of DB_Connection &#8212; if it is not, we return the current instance.</p>
<p>Static variables work by declaring the static keyword, then continuing on with the normal variable initialization.  The variable will maintain it&#8217;s assigned value (whether changed or not) throughout the life of the script.</p>
<p>The second time the getInstance() is called, the $instance variable contains the instance of DB_Connection, and we will return that value.  Regardless of how many instances are created of this class, only one object is initialized.</p>
<p><strong>Conclusion</strong></p>
<p>As you can see, the singleton pattern certainly has it&#8217;s uses, albeit specific.  Keeping the OOP design patterns in mind makes for a cleaner, easier to maintain code base for future developers picking up your projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Namespace Update</title>
		<link>http://www.willfitch.com/php-namespace-update.html</link>
		<comments>http://www.willfitch.com/php-namespace-update.html#comments</comments>
		<pubDate>Mon, 10 Nov 2008 03:50:25 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=105</guid>
		<description><![CDATA[Since the writing of my namespace post things have changed. The voting is in (so it&#8217;s said) and the new namespace separator is the backslash &#8220;\&#8221; character. That&#8217;s right, the escape sequence character. This is caused an uproar in the community and I&#8217;m personally not a fan of it. At the same time (and after [...]]]></description>
			<content:encoded><![CDATA[<p>Since the writing of my <a href="http://www.phpfever.com/using-php-namespaces.html">namespace post</a> things have changed.  The voting is in (so it&#8217;s said) and the new namespace separator is the backslash &#8220;\&#8221; character.  That&#8217;s right, the escape sequence character.</p>
<p>This is caused an uproar in the community and I&#8217;m personally not a fan of it.  At the same time (and after many weeks of debating), I have decided to support this effort.  While I&#8217;m not a fan of its implementation, I&#8217;m still pleased that namespace functionality is being implemented into PHP.</p>
<p>Your thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/php-namespace-update.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

