<?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; php5</title>
	<atom:link href="http://www.willfitch.com/tag/php5/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>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>Using PHP Namespaces</title>
		<link>http://www.willfitch.com/using-php-namespaces.html</link>
		<comments>http://www.willfitch.com/using-php-namespaces.html#comments</comments>
		<pubDate>Thu, 24 Apr 2008 22:43:03 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php5]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=49</guid>
		<description><![CDATA[PHP 5.3 introduces a much requested feature for object-oriented programmers: namespaces. At the time of this writing, version 5.3 of PHP was in development, but is planned on being released in the near future. One of the purposes object-oriented programming is to remove ambiguous development and data access items. This basically means identifying common functionality [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5.3 introduces a much requested feature for object-oriented programmers: namespaces.  At the time of this writing, version 5.3 of PHP was in development, but is planned on being released in the near future.</p>
<p>One of the purposes object-oriented programming is to remove ambiguous development and data access items.  This basically means identifying common functionality and creating the most reusable framework possible, typically in the form of classes.  When creating this functionality, you will begin to have issues with naming conventions and narrowing down functionality even further.  To resolve this scoping issue, namespaces allow you to contain those bits of code even more.</p>
<p><span id="more-247"></span></p>
<p><strong>UPDATE</strong></p>
<p>Since the writing of this article, PHP has changed the namespace separator to a new character.  <a href="/php-namespace-update.html">Please view my update to this article.</a></p>
<p><strong><u>Getting Your Environment Setup</u></strong></p>
<p>Before going any further, you will need to download a release of PHP greater than or equal to 5.3.  Since 5.3 is not currently production ready, you can download one of the development releases at <a href="http://snaps.php.net/">snaps.php.net</a>.  Once you have that installed, you can use the examples described in this article.</p>
<p><strong><u>Should I Use Namespaces?</u></strong></p>
<p>That is a question that can cause controversy among developers.  Namespaces are a solution for grouping large sets of libraries and common functionality.  The keyword being &#8220;<strong>large</strong>&#8220;.  You need to be realistic about this.  While not definitive, here are some good signs that namespaces are right for you:</p>
<ol>
<li>Your application is not on shared hosting.</li>
<li>You have so many libraries, or your framework requires less work by adding class paths to the include_path php.ini directive.</li>
<li>More than one developer is working on the application (debatable)</li>
<li>You have an existing MVC framework (Code Igniter, Symfony, Zend Framework) that your application is developed on.</li>
</ol>
<p>Namespaces really can be handy, but they can also cause confusion.  Just as you group methods and properties in a class for common, relational functionality, you may group classes and functions even further.  However, there are some rules that you must follow.</p>
<p><strong><u>Now for the Rules</u></strong></p>
<p>Here are the rules from <a href="http://us2.php.net/manual/en/language.namespaces.rules.php">php.net</a>:</p>
<ol>
<li>All qualified names are translated during compilation according to current import rules. In example, if the namespace A::B::C is imported, a call to C::D::e() is translated to A::B::C::D::e().</li>
<li>Unqualified class names are translated during compilation according to current import rules (full name substituted for short imported name). In example, if the namespace A::B::C is imported, new C() is translated to new A::B::C().</li>
<li>Inside namespace, calls to unqualified functions that are defined in the current namespace (and are known at the time the call is parsed) are interpreted as calls to these namespace functions, at compile time.</li>
<li>Inside namespace (say A::B), calls to unqualified functions that are not defined in current namespace are resolved at run-time. Here is how a call to function foo() is resolved:
<ol>
<li>It looks for a function from the current namespace: A::B::foo().</li>
<li>It tries to find and call the internal function foo().</li>
</ol>
<p>      To call a user defined function in the global namespace, ::foo() has to be used.
</li>
<li>Inside namespace (say A::B), calls to unqualified class names are resolved at run-time. Here is how a call to new C() is resolved:
<ol>
<li>It looks for a class from the current namespace: A::B::C.</li>
<li>It tries to find and call the internal class C.</li>
<li>It attemts to autoload A::B::C.</li>
</ol>
<p>      To reference a user defined class in the global namespace, new ::C() has to be used.
</li>
<li>
Calls to qualified functions are resolved at run-time. Here is how a call to A::B::foo() is resolved:</p>
<ol>
<li>It looks for a function foo() in the namespace A::B.</li>
<li>It looks for a class A::B and call its static method foo(). It will autoload the class if necessary.</li>
</ol>
</li>
<li>Qualified class names are resolved in compile-time as class from corresponding namespace. For example, new A::B::C() refers to class C from namespace >A::B.</li>
</ol>
<p><strong><u>Okay, so How Do I Use Them?</u></strong></p>
<p>It seems that PHP will be going with a similar namespace setup as C++.  In order to declare a namespace, you will use the &#8220;namespace&#8221; keyword.</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;">namespace</span> MyNamespace<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Test <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello'</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>The declaration above simply states that all elements contained in this script will be referenced with the &#8220;MyNamespace&#8221; namespace.  You will need to place the &#8220;namespace&#8221; declaration at the top of the script.</p>
<p>In order to use a portion of functionality within this script, we will use the <a href="http://us2.php.net/manual/en/language.oop5.paamayim-nekudotayim.php">Scope Resolution Operator</a> and instantiate the &#8220;Test&#8221; class.</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: #666666; font-style: italic;">// Requiring the namespace file is a good indication that you don't need to use namespaces, but this is only an example!</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_file_above.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyNamespace<span style="color: #339933;">::</span><span style="color: #004000;">Test</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Prints 'hello'</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Pretty nifty, huh?  Okay, so let&#8217;s get a little deeper into this.  What if you have a complicated set of namespaces that are three or four levels deep?  Is this a viable solution?</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: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Level1<span style="color: #339933;">::</span><span style="color: #004000;">Level2</span><span style="color: #339933;">::</span><span style="color: #004000;">Level3</span><span style="color: #339933;">::</span><span style="color: #004000;">SomeClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doSomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$object2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Level1<span style="color: #339933;">::</span><span style="color: #004000;">Level2</span><span style="color: #339933;">::</span><span style="color: #004000;">Level3</span><span style="color: #339933;">::</span><span style="color: #004000;">AnotherClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$object2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">imTiredOfTyping</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;">?&gt;</span></pre></div></div>

<p>In case you&#8217;re wondering, the answer is <strong>NO</strong>.  Instead, we can alias a namespace to save some keystrokes and future arthritis pains:</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;">use</span> Level1<span style="color: #339933;">::</span><span style="color: #004000;">Level2</span><span style="color: #339933;">::</span><span style="color: #004000;">Level3</span> <span style="color: #b1b100;">as</span> SomeAlias<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SomeAlias<span style="color: #339933;">::</span><span style="color: #004000;">SomeClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$object2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SomeAlias<span style="color: #339933;">::</span><span style="color: #004000;">AnotherClass</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;">?&gt;</span></pre></div></div>

<p>That saved some time!  Now, how do we get that many levels of namespaces and why?  Well, the more complicated your framework and libraries become, the more creative naming and depth of namespaces become.  For instance, let&#8217;s consider a namespace that handles database actions.  You may have a MySQL, Postgres, and MS SQL set of drivers.  You could setup your namespace levels like this:</p>
<ul>
<li>Database</li>
<ul>
<li>Mysql</li>
<ul>
<li>ActiveRecord</li>
</ul>
<li>Pgsql</li>
<ul>
<li>ActiveRecord</li>
</ul>
<li>Mssql</li>
<ul>
<li>ActiveRecord</li>
</ul>
</ul>
</ul>
<p>In this example, albeit not the best, your namespaces could be setup in a relational manner:</p>
<p>Database::Mysql::ActiveRecord;<br />
Database::Pgsql::ActiveRecord;<br />
Database::Mssql::ActiveRecord;</p>
<p>Within the ActiveRecord namespace, you could have a class that handles common database transactions:</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;">namespace</span> Database<span style="color: #339933;">::</span><span style="color: #990000;">Mysql</span><span style="color: #339933;">::</span><span style="color: #004000;">ActiveRecord</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Manager  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> select<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 a select</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> update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$table</span><span style="color: #339933;">,</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// do an update</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>You could then have a directory outside of the web root called &#8220;database&#8221; which contains each of these DB namespaces, and add that to your INI &#8220;include_path&#8221; directive.  This makes a large system a little better organized and maintainable.  Just remember, namespaces add another layer of complexity to your codebase, so make sure your organization creates standards of developing namespaces.</p>
<p>You can also alias a class within a namespace for use:</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;">use</span> Database<span style="color: #339933;">::</span><span style="color: #990000;">Mysql</span><span style="color: #339933;">::</span><span style="color: #004000;">ActiveRecord</span><span style="color: #339933;">::</span><span style="color: #004000;">Manager</span> <span style="color: #b1b100;">as</span> RecordManager<span style="color: #339933;">;</span>
<span style="color: #000088;">$manager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RecordManager<span style="color: #009900;">&#40;</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>This scenario is handy if you find yourself instantiating a class multiple times.  Declare the use of the namespace all the way to the class level, alias the class, then use it as you would as a normal instantiation.</p>
<p><strong><u>Global Space</u></strong></p>
<p>You can also redeclare PHP functionality that is available in the global space.  Normally you can&#8217;t redeclare a function name without producing a fatal error.  In the case of namespaces, it is possible!</p>
<p>Let&#8217;s say you want to recreate the function <a href="http://www.php.net/print_r">print_r</a>.  You might want to add a check to see if the script running is web or CLI (Command Line Interface).  You can do so by prefixing the PHP function with the Scope Resolution Operator:</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;">namespace</span> String<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Assume CLI</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">::</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Add &lt;pre&gt; tags for better output to a browser</span>
        <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;pre&gt;%s&lt; /pre&gt;'</span><span style="color: #339933;">,</span> <span style="color: #339933;">::</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</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>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You could then call this function:</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: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'String.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
String<span style="color: #339933;">::</span><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Test text'</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>This allows you to &#8220;redeclare&#8221; print_r.</p>
<p><strong><u>Conclusion</u></strong></p>
<p>As I took a glance at the upcoming namespace functionality in PHP 5.3, I was intrigued, but a little disappointed.  I was really hoping that PHP would implement similar functionality to C#, but PHP is a very well-developed language that is constantly being improved, and the OOP implementation between versions 4 and 5 can account for that.  I have high hopes that PHP will increase the functionality of namespaces, and satisfy users that have been using them for years with other tools.  There is no doubt in my mind that PHP is heading in the right direction!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/using-php-namespaces.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

