<?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"
	>

<channel>
	<title>Developer Tutorials' Webmaster Blog &#187; PHP SPL</title>
	<atom:link href="http://www.developertutorials.com/blog/tag/php-spl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.developertutorials.com/blog</link>
	<description>Keeping webmasters up-to-date on technology.</description>
	<pubDate>Tue, 02 Sep 2008 14:59:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Iterating PHP objects, and readable code too!</title>
		<link>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/</link>
		<comments>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 12:22:17 +0000</pubDate>
		<dc:creator>Akash Mehta</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP SPL]]></category>

		<guid isPermaLink="false">http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/</guid>
		<description><![CDATA[Today I&#8217;m going to take a look at object iteration, most commonly found in the Standard PHP Library, and explore using the Iterator interface to simplify looping.

It&#8217;s a generally accepted fact that more readable code is more maintainable and easier for other developers to pick up. Simple scripts can be easily worked around these principles, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m going to take a look at object iteration, most commonly found in the <a target="_blank" href="http://www.developertutorials.com/php-manual/ref.spl.html">Standard PHP Library</a>, and explore using the Iterator interface to simplify looping.</p>
<p><span id="more-150"></span></p>
<p>It&#8217;s a generally accepted fact that more readable code is more maintainable and easier for other developers to pick up. Simple scripts can be easily worked around these principles, but the moment we explore any complexity the intricacies of the language impede basic readability - just look at the average Perl script, for example.</p>
<p>The <a target="_blank" href="http://www.developertutorials.com/php-manual/ref.spl.html">Standard PHP Library (SPL)</a> religiously utilises iteration for some outstanding results (and great code simplicity). PHP&#8217;s native language constructs are very readable, and by simplifying our code we can build applications faster and more effectively. The SPL demonstrates this with the power of its classes integrating with language constructs, such as the foreach loop.</p>
<p>Looping is probably one of the most common elements of PHP applications. Take this simple example, for instance:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$vegetables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tomato&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;carrot&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cauliflower&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$vegetables</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$vegetable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">print</span> <span style="color: #000088;">$vegetable</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see, this is a very simple example, and probably doesn&#8217;t reflect a lot of the loops in your day-to-day development. They might look a little more like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$records</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> RECORDS_PER_PAGE<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">print</span> <span style="color: #000088;">$row</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s a bit of an exaggeration - assignments in a foreach loop is not a good idea and (thankfully) isn&#8217;t very common - but it demonstrates the fundamental problem with common uses of loops - they stop making sense.</p>
<p>What if we could improve that? Consider this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Where $db is an object. That&#8217;s right, iterating over an object. Now, typically when we iterate over an object, we get an array of its public properties. In this case, we can control from within the object $db precisely what we get when we iterate over it, by implementing the Iterator interface.</p>
<p>This can work just fine, as when you iterate over an object, you can probably write a little bit of code within that object so that it knows what you wanted in the previous more complex example. For example, if you&#8217;re running a database query then fetching the resultset, you can instead run the database query then &#8220;fetch&#8221; the entire database object. When you iterate over the database object, it now knows that you really want to iterate over the resultset - and it can do just that.</p>
<p>Here&#8217;s an example of the class that $db might be an instance of:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> MyDatabase implements Iterator
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Result 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Result 2&quot;</span><span style="color: #009900;">&#41;</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> <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: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</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> <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: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</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> <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: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</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> <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: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</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> valid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</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: #000000; font-weight: bold;">false</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$var</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>As you can see, it&#8217;s a perfectly normal class, implementing methods with names close to the standard array functions. When your object is iterated over, these Iterator methods will be called where needed. What actually happens inside the methods is up to you &#8212; in this instance, we store the data in an array, $this->data, but it could as well be a complex recordset or XML node tree. As long as calling the appropriate methods performs the action needed, our class can manage its data internally and all we have to do is iterate over it like any other PHP array.</p>
<p>And just for readability, we could always go a step further:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$books</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$db</span>; <span style="color: #666666; font-style: italic;">// $books is now our $db object</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$book</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now isn&#8217;t that what you&#8217;d like to see when working with someone else&#8217;s code?</p>
<p>For further reading on object iteration, check out the <a target="_blank" href="http://developertutorials.com/php-manual/language.oop5.iterations.html">manual page on iterating objects</a>. Also take a look the <a target="_blank" href="http://developertutorials.com/php-manual/ref.spl.html">standard PHP library</a> functions.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark" style="float:left;">
<div class="d150" style="overflow:hidden">
<div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/&amp;title=Iterating+PHP+objects%2C+and+readable+code+too%21" title="Add to&nbsp;Del.icio.us">Del.icio.us</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/&amp;title=Iterating+PHP+objects%2C+and+readable+code+too%21" title="Add to&nbsp;Digg This">Digg This</a></div><div id="socialstyles"><a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/&amp;title=Iterating+PHP+objects%2C+and+readable+code+too%21" title="Add to&nbsp;Stumble">Stumble</a></div></div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.developertutorials.com/blog/php/iterating-php-objects-spl-iterato-150/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
