<?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/category/articles/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>PHP Typehint Return Values</title>
		<link>http://www.willfitch.com/php-typehint-return-values.html</link>
		<comments>http://www.willfitch.com/php-typehint-return-values.html#comments</comments>
		<pubDate>Sun, 11 Dec 2011 04:59:02 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.willfitch.com/?p=322</guid>
		<description><![CDATA[As some of you know, I&#8217;ve been working for a while on implementing type hinting for method return values. I&#8217;m pleased to say that the implementation is done. The following types are allowed: Array Class names Parameter type hints allow callable types, but until a keyword is assigned to &#8220;callable&#8221; (RFC), it&#8217;s not possible to [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you know, I&#8217;ve been working for a while on implementing type hinting for method return values.  I&#8217;m pleased to say that the implementation is done.  </p>
<p>The following types are allowed:</p>
<ul>
<li>Array</li>
<li>Class names</li>
</ul>
<p>Parameter type hints allow callable types, but until a keyword is assigned to &#8220;callable&#8221; <a href="https://wiki.php.net/rfc/callable">(RFC)</a>, it&#8217;s not possible to implement this for method returns.  To be honest, I&#8217;m not completely sold that it <strong>should</strong> be implemented at all.  That&#8217;s another conversation.</p>
<p>The syntax is simple &#8211; rather than declaring &#8220;function&#8221;, you replace it with array or your class name.  Since mixed types are allowed, you can still use &#8220;function&#8221;.</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> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ArrayIterator getIterator<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: #000000; font-weight: bold;">new</span> ArrayIterator<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: #990000;">array</span> getArray<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;">'I am returning an array'</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> thisStillWorks<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: #0000ff;">'This still works, too.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The same rules and errors for type hint parameters apply if you attempt to return a value not specified by the return type; a catchable fatal error will be thrown.  Note that this is <strong>NOT</strong> an exception.</p>
<p><strong>Reflection</strong></p>
<p>I added &#8220;getReturnType()&#8221; to the ReflectionMethod class.  It will return one of these three values: &#8220;mixed&#8221; &#8211; for function, &#8220;array&#8221; or &#8220;ClassName&#8221; if an object is specified.</p>
<p><strong>Interfaces</strong></p>
<p><strike>Interface implementation is incomplete.  I will be working on that this week. That said, if you specify an interface on the return type, it works just fine.  The only thing not implemented is validation on overloading interface methods.</strike></p>
<p>Interfaces and type hinting return values are fully implemented now.  If, for example, you implement an interface which defines a method that returns an array, and you attempt to change it, an E_COMPILE_ERROR is raised.</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> MyInterface
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blah<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;">class</span> YourClass implements MyInterface
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blah<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: #0000ff;">'works!'</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> MyClass
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> MyInterface getValue<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: #000000; font-weight: bold;">new</span> YourClass<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></pre></div></div>

<p>The above works and validates correctly.</p>
<p><strong>Help</strong></p>
<p>If you&#8217;re interested in testing out this patch, shoot me an email using the <a href="http://www.willfitch.com/about">contact page</a>.  If you&#8217;d just like to play with the functionality, go to <a href="https://github.com/fitchwh" title="my GitHub page">my GutHub page</a> and <a href="http://github.com/fitchwh/php-src">download</a> or <a href="git@github.com:fitchwh/php-src.git">clone</a> the repo.  The branch I&#8217;m working out of is &#8220;returntype&#8221;.  Just make sure you compile with &#8211;enable-debug.</p>
<p>If you happen to run into any memory leaks, please provide any information returned from the output.</p>
<p>Until next time. <img src='http://www.willfitch.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/php-typehint-return-values.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>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>Zip Code Web Service Updates</title>
		<link>http://www.willfitch.com/zip-code-web-service-updates.html</link>
		<comments>http://www.willfitch.com/zip-code-web-service-updates.html#comments</comments>
		<pubDate>Tue, 13 Jan 2009 01:11:14 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=111</guid>
		<description><![CDATA[I have updated the zip code web service to fix some issues with the data returned from getLocalTime. Prior to this fix, only the 12 hour format was displayed without the proper meridiem (AM or PM). The time was also off by one hour. This fix adds two additional children: currentMilitaryTime &#8211; The 24 hour [...]]]></description>
			<content:encoded><![CDATA[<p>I have updated the <a href="/soap-zip-code-web-service.html">zip code web service</a> to fix some issues with the data returned from getLocalTime.  Prior to this fix, only the 12 hour format was displayed without the proper meridiem (AM or PM).  The time was also off by one hour.</p>
<p>This fix adds two additional children:</p>
<ol>
<li>currentMilitaryTime &#8211; The 24 hour time format</li>
<li>currentMeridiemTime &#8211; The 12 hour time format with AM or PM</li>
</ol>
<p>I have left the currentTime element for backwards compatibility.  I do plan on removing it when I release the new zip code web service some time this month.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/zip-code-web-service-updates.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Zend Certification Exam: What to Expect</title>
		<link>http://www.willfitch.com/zce-what-to-expect.html</link>
		<comments>http://www.willfitch.com/zce-what-to-expect.html#comments</comments>
		<pubDate>Mon, 28 Apr 2008 02:55:47 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Certification Exam]]></category>
		<category><![CDATA[php certification]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=51</guid>
		<description><![CDATA[The Zend Certification Exam is a step in the right direction for PHP developers looking to enhance their resume and prove their skills. It is becoming accepted by recruiters worldwide and in some cases, demanded by corporations seeking employees. Achieving this certification is no easy task, though. It requires working experience with PHP, as well [...]]]></description>
			<content:encoded><![CDATA[<p>The Zend Certification Exam is a step in the right direction for PHP developers looking to enhance their resume and prove their skills.  It is becoming accepted by recruiters worldwide and in some cases, demanded by corporations seeking employees.  Achieving this certification is no easy task, though.  It requires working experience with PHP, as well as the technologies associated with it.</p>
<p>The test consists of 70 questions and you have 90 minutes to complete them.  You may skip between questions and revisit those that you skipped.  You will likely finish the examination in 30 to 45 minutes (purely personal experience), so take the other half and check your answers.</p>
<p><span id="more-248"></span></p>
<p><strong><u>What Should I Know Before Taking the Test?</u></strong></p>
<p>It is suggested to have at least six months of experience as a full-time PHP developer.  This includes working on a day-to-day basis with technologies that integrate with PHP such as databases and regular expressions.</p>
<p>The test is based on your ability to evaluate and analyze a snippet of code.  You will receive many questions showing you just this, and it will ask for an output response, whether there&#8217;s an error produced or both.  Most questions are multiple-choice, but there are some that will expect you to either type the output of the code or a function name into a text box.</p>
<p>Here are a list of topics that are covered on the test:</p>
<ul>
<li>PHP basics</li>
<li>Functions</li>
<li>Arrays</li>
<li><strong>Object-oriented programming</strong></li>
<li>Security</li>
<li>XML and web services</li>
<li>Strings and patterns</li>
<li>Databases and SQL (that&#8217;s right, SQL)</li>
<li>Web features</li>
<li>Streams and network programming</li>
<li>PHP 4/5 differences</li>
<li>Design and theory</li>
</ul>
<p>I know this is a very broad range of topics, but the test does base your skills on an intermediate to advanced PHP programmer.  Don&#8217;t let this scare you off, however.  You don&#8217;t have to be an expert in these areas, just familiarize yourself with them.</p>
<p>Do notice the words &#8220;object-oriented programming&#8221; in bold.  Since PHP5&#8242;s greatest feature is its object model, you should expect lots of questions relating to it.  This does include object-oriented design and theory (the last category noted).</p>
<p><strong><u>How Much Does the Test Cost?</u></strong></p>
<p>The test costs US $125.  You can purchase a voucher at <a href="http://www.zend.com/en/store/php-certification/">http://www.zend.com/en/store/php-certification/</a>.</p>
<p><strong><u>What&#8217;s the Best Way to Study for the Test?</u></strong></p>
<p>Visit the official <a href="http://www.zend.com/en/services/certification/php5-certification">Zend description of the test</a>.  Visit the php.net documentation for each of those categories, and start coding.  Reading does help, but you need to get your hands dirty with actual, real-life development scenarios.</p>
<p>When you think you are ready, purchase a block of practice exams <a href="http://www.zend.com/en/store/php-certification/online-practice-testing">here</a>.  I can tell you that these practice exams are very similar to the actual test.</p>
<p><strong><u>When Will I Know if I Pass?</u></strong></p>
<p>After submitting the test for grading, you will receive a grade of &#8220;PASS&#8221; or &#8220;FAIL&#8221;.  The actual numeric grade is never disclosed.</p>
<p><strong><u>What if I Fail?</u></strong></p>
<p>The test will give you a detailed explanation of the weaknesses of your evaluation.  These are grouped by the categories listed above.  Take the advice of the evaluation explanation and study those categories harder.  Don&#8217;t worry.  Just get back up on your feet and try again.  Zend does give a discount to those who fail the test.</p>
<p><strong><u>I Have More Questions!</u></strong></p>
<p>If you have any questions, I&#8217;ll be glad to address them as best I can.  Feel free to ask them in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/zce-what-to-expect.html/feed</wfw:commentRss>
		<slash:comments>12</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>

