<?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>Sunstorm Blog &#187; PHP</title>
	<atom:link href="http://www.sunstormlabs.net/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sunstormlabs.net/blog</link>
	<description>We make the storm in your head</description>
	<lastBuildDate>Thu, 17 Dec 2009 17:30:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend Studio for Eclipse</title>
		<link>http://www.sunstormlabs.net/blog/2009/06/06/zend-studio-for-eclipse/</link>
		<comments>http://www.sunstormlabs.net/blog/2009/06/06/zend-studio-for-eclipse/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 02:22:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/2009/06/06/zend-studio-for-eclipse/</guid>
		<description><![CDATA[Right, lets get this out of the way. Zend Studio for Eclipse is a great tool and no other PHP IDEs come near to its level of Intellisense smarts, features, and integration. I’ve been using it steadily since version 5, before it was Eclipse, and I don’t see myself stopping in the future.
That said…
Most code [...]]]></description>
			<content:encoded><![CDATA[<p>Right, lets get this out of the way. Zend Studio for Eclipse is a great tool and no other PHP IDEs come near to its level of Intellisense smarts, features, and integration. I’ve been using it steadily since version 5, before it was Eclipse, and I don’t see myself stopping in the future.</p>
<p>That said…</p>
<p>Most code editors have figured out this whole code folding thing where you click on the minus button next to the function block and it collapses into a neat declaration.</p>
<p>Zend Studio has this too, only with a slight twist. Most of the times it works right. Other times, it forgets to fold bits. Yet other times, especially when folding PHPDoc blocks, the fold swallows whole unrelated fields/functions that happen to be next to the one I want to fold, declaration and everything. Nice.</p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="zend-collapse-faggotry" border="0" alt="zend-collapse-faggotry" src="http://www.sunstormlabs.net/blog/wp-content/uploads/2009/06/zendcollapsefaggotry.png" width="504" height="95" /> </p>
<p>I could understand this slipping past testing given how intermittent it is, but ZS’s been around for at least a couple of years, and it’s got like a dozen version updates, one of which explicitly touted having better folding. Yeah, it’s better, because before it screwed up half the time rather than a 10th of the time. It still sucks.</p>
<p>And while on the subject of sucking, in what universe is it sensible that if I edit a file that exists in a ZS project outside of the environment, and then try to open it in ZS, it flips out in glorious fashion:</p>
<p>&#160;<a href="http://www.sunstormlabs.net/blog/wp-content/uploads/2009/06/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.sunstormlabs.net/blog/wp-content/uploads/2009/06/image-thumb.png" width="743" height="592" /></a> </p>
<p>And then I right click on the offending file, click on Refresh, and it’s happy again. If it knows it’s out of sync, why the hell can’t it refresh it automatically? The file’s <em>right there</em>!</p>
<p>Makes me want to hit it. Surely I can’t be the only one running into this stuff?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2009/06/06/zend-studio-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>register_shutdown_function after all the other register_shutdown_functions</title>
		<link>http://www.sunstormlabs.net/blog/2008/03/14/shutdown_function-after-all-the-other-functions/</link>
		<comments>http://www.sunstormlabs.net/blog/2008/03/14/shutdown_function-after-all-the-other-functions/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 16:35:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=73</guid>
		<description><![CDATA[PHP&#8217;s register_shutdown_function lets me run code after it finishes running the script, be that naturally or forcibly. This makes it a perfect fit for a whole bunch of things, like running maintenance database queries or showing debugging information.

function do_maintenance&#40;&#41;
&#123;
  $db-&#38;gt;doSomeQueries&#40;&#41;;
&#125;
&#160;
function debug_write&#40;&#41;
&#123;
  echo 'some debugging information';
&#125;
&#160;
// Last things to execute
register_shutdown_function&#40; 'do_maintenance' &#41;;
register_shutdown_function&#40; 'debug_write' &#41;;

There&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>PHP&#8217;s <code>register_shutdown_function</code> lets me run code after it finishes running the script, be that naturally or forcibly. This makes it a perfect fit for a whole bunch of things, like running maintenance database queries or showing debugging information.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> do_maintenance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>doSomeQueries<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;">function</span> debug_write<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;">'some debugging information'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Last things to execute</span>
<span style="color: #990000;">register_shutdown_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'do_maintenance'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">register_shutdown_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'debug_write'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There&#8217;s one caveat, and it&#8217;s explained in the PHP documentation:</p>
<blockquote><p>Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered.</p></blockquote>
<p><span id="more-73"></span></p>
<p>This means that if I also want to see debug output from the queries in the <code>do_maintenance</code> function, I have to make sure that debug_write is always the <em>last registered function</em>.</p>
<p>This can be inconvenient. Luckily, PHP has a function that allows a script to run code after it&#8217;s finished all of it&#8217;s processing&#8230; And it lets me register another shutdown function inside that function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">register_shutdown_function</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'register_shutdown_function(\'debug_write\');'</span> <span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Really last thing to execute</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #990000;">register_shutdown_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'do_maintenance'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Last thing to execute</span></pre></div></div>

<p>This assures that the function that it registered will always be the last one, and this way, despite having the debug function registered before the maintenance function, the debug function will still execute last.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2008/03/14/shutdown_function-after-all-the-other-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What good are ArrayObjects?</title>
		<link>http://www.sunstormlabs.net/blog/2008/02/29/what-good-are-arrayobjects/</link>
		<comments>http://www.sunstormlabs.net/blog/2008/02/29/what-good-are-arrayobjects/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 16:00:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[arrayobject]]></category>

		<guid isPermaLink="false">http://www.sunstormlabs.net/blog/?p=12</guid>
		<description><![CDATA[Ever since I had my first encounter with the SPL, it took me a while to understand the actual usefulness of the ArrayObject. Its use isn&#8217;t obvious, because it only reveals itself if you want to follow a strict discipline of class encapsulation. Otherwise it doesn&#8217;t offer any advantage over the standard PHP array.
A lot [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I had my first encounter with the SPL, it took me a while to understand the actual usefulness of the <a href="http://www.php.net/~helly/php/ext/spl/classArrayObject.html">ArrayObject</a>. Its use isn&#8217;t obvious, because it only reveals itself if you want to follow a strict discipline of class encapsulation. Otherwise it doesn&#8217;t offer any advantage over the standard PHP array.</p>
<p>A lot of the confusion comes from the lack of documentation. An ArrayObject, the documentation says, is <em>&#8220;an array wrapper&#8221;</em>, that <em>&#8220;allows to recursively iterate over Arrays and public Object properties&#8221;</em>. This isn&#8217;t very useful, and it doesn&#8217;t explain at all what is the ArrayObject actually good for. Iteration over arrays and objects has been long supported simply with the foreach construct:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$letter</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is how you&#8217;d do it with an ArrayObject:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arrObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arrObject</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arrObject</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arrObject</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$arrObject</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$letter</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-12"></span><br />
Both snippets have the same output. You can use the ArrayObject just like you would use the array because it implements several interfaces from the SPL:</p>
<ul>
<li>The <code>IteratorAggregate</code> interface, which lets foreach iterate over its contents by returning an <code>Iterator</code></li>
<li>The <code>Countable</code> interface, that tells the <code>count()</code> function what to return when used on the object</li>
<li>And the <code>ArrayAccess</code> interface, that gives an object the ability to be accessed using the array syntax.</li>
</ul>
<p>These interfaces are very useful when implemented in your objects, but for the purpose of a simple &#8220;array wrapper&#8221;, it just adds the overhead of having an object, and then an iterator object to iterate over it. Yet, its <a href="http://devzone.zend.com/article/2565-The-Standard-PHP-Library-SPL#section4-2">exactly what Zend is encouraging</a>:</p>
<blockquote><p>The ArrayObject class is a powerful tool for iterating through and accessing arrays. Its practical uses are infinite; use it as a wrapper for all arrays: simple arrays, arrays of database records, arrays of objects, etc. Consider the listing in Example 9. Continuing the PartyMember examples from the earlier listings, I fetch data from a database, creating an array of PartyMember objects, from which I then create an ArrayObject object. While this example shows simple iteration with a foreach loop (which I could do when it was a normal array), using ArrayObject provides access to tools that are not accessible to non-object arrays <strong>without the use of procedural code and functions</strong>. For example, with the ArrayObject, it is possible to sort the array, modify elements or append new ones, and iterate through the results.</p></blockquote>
<p>This is nice and all, but the only concrete reason they give for using an ArrayObject is, apparently, to perpetuate the object oriented master-race, while oppressing those horrible, evil and icky <em>functions</em>. And this is good enough reason if you&#8217;re an OOP purist, and there&#8217;s nothing wrong with dreaming of a nice, <a href="http://www.rubyonrails.org/">object oriented, well structured PHP</a>. For me, it just doesn&#8217;t cut it. There are reasons why the ArrayObject is inferior in this purpose.</p>
<p>While the overhead is probably insignificant, there&#8217;s the thing that an ArrayObject isn&#8217;t actually an array, it just behaves like one on occasion. This means that it&#8217;s not compatible with most places where you&#8217;d normally use an array. You can&#8217;t, for example, use most of PHP&#8217;s <a href="http://www.php.net/array">array functions</a> on it directly. Some of the functions, like sorting, are available in the class, but most of them aren&#8217;t. For those, you have to extract the internal array, do your operations on it, and then exchange it back in. For me, this seems too much to bother:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arrObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arrObject</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getArrayCopy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arrObject</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>exchangeArray<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>So what good is the ArrayObject?</h2>
<p>Like I said before, it&#8217;s got to do with class encapsulation.</p>
<p>We&#8217;ve all heard how it&#8217;s Evil to directly expose properties of a class. You may or may not accept this as an evangelical truth, <span title="and if you don't, ArrayObjects are still useless">but if you do</span>, you might have come across a situation where you need to allow outsiders to interact with an array in your 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> MyClass
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</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>
  <span style="color: #000000; font-weight: bold;">public</span> getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first problem arises in the fact that arrays are passed by value in PHP, meaning, a copy of the array will be passed out from the function, and that means that changing the result of the <code>getList()</code> method won&#8217;t actually change the values in your class. To be able to write to the array, you can either provide a setter, implement methods in your class to do adding and removing from the array or pass it out by reference.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClassWithSetter
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</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>
  <span style="color: #000000; font-weight: bold;">public</span> getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> setList<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$list</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>list <span style="color: #339933;">=</span> <span style="color: #000088;">$list</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> MyClassWithAccessors
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</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>
  <span style="color: #000000; font-weight: bold;">public</span> getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> addToList<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$item</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</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> MyClassByReference
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</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>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Note the &amp;amp;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Neither of these options are very attractive. The first one requires extra code on the outside to get the array, and then set it back in. The second one forces you to implement array access code for every array you want to expose, and the third one is basically exposing the internal array to any kind of modification. There&#8217;s nothing stopping someone from doing something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClassByReference<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$list</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$list</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Since <code>$list</code> is a reference, unsetting it will also unset <code>$mc</code>&#8217;s <code>$list</code>, and unless you check for that everywhere you use it, it can be catastrophic.</p>
<p>And then, there&#8217;s the problem of actually using the class. Regardless of whether you pass as a value or as a reference, you still can&#8217;t access it without putting it into a variable on its own. This, for example, is invalid syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClassByReference<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Item 1'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Invalid</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'Item 1'</span> <span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Invalid</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Invalid</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The right way to do this would be:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClassByReference<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$list</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$list</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Item 1'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$list</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'Item 1'</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$list</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Tolerable, but not optimal.</p>
<h2>ArrayObject to the rescue</h2>
<p>The solution comes, dragged right out of other OOP languages, in the form of the ArrayObject. Objects, in PHP5, by default, are passed around between functions by making a <strong>copy of the reference</strong>. That means that if you pass out an object variable out of a function, while it&#8217;s a copy of the original variable, it still points to <strong>the same instance</strong> of the object. This means we no longer have to pass by reference when we expose our list!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClassWithArrayObject
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</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;">-&amp;</span>gt<span style="color: #339933;">;</span>list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<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> getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$mc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClassWithArrayObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>append<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Item 1'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getOffset<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'Item 1'</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>unsetOffset<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And because the ArrayObject has the OOP interface, you no longer have to extract it into a local variable first!</p>
<p>We could stop here, but we still have the issue of someone passing in invalid data into the list. For this, we can simply extend our ArrayObject.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> StringArrayObject <span style="color: #000000; font-weight: bold;">extends</span> ArrayObject
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> append<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$value</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: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Value must be a string'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$value</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> offsetSet<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newval</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: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$newval</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Newval must be a string'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    parent<span style="color: #339933;">::</span><span style="color: #004000;">offsetSet</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #000088;">$newval</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> exchangeArray<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</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;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</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: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Value must be a string'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    parent<span style="color: #339933;">::</span><span style="color: #004000;">exchangeArray</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</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> MyClass
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$list</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> __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;">-&amp;</span>gt<span style="color: #339933;">;</span>list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringArrayObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getList<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;">-&amp;</span>gt<span style="color: #339933;">;</span>list<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$mc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// these will work.</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>append<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'item'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>exchangeArray<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'item'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// these won't.</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>append<span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">123</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mc</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>exchangeArray<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">123</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'item'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is much better. Obviously, you can include any kind of validation logic you want.</p>
<p>It&#8217;s not a perfect world. For one thing, I would still prefer to use &#8220;<code>[]</code>&#8221; rather than &#8220;<code>append()</code>&#8220;, and &#8220;<code>[0]</code>&#8221; instead of &#8220;<code>offsetGet(0)</code>&#8220;. For this, I could use PHP&#8217;s <a title="Not overloading at all" href="http://www.php.net/manual/en/language.oop5.overloading.php">&#8220;overloading&#8221;*</a> to make actual accessors, but I personally think it&#8217;s ugly and stupid having to implement plumbing logic for my class that should be in the language itself. Maybe one day PHP will have proper accessors, or support the &#8220;<code>getList()[]</code>&#8221; syntax.</p>
<p>Furthermore, there&#8217;s the issue that the ArrayObject doesn&#8217;t support most of the array manipulation functions of PHP, so it certainly can&#8217;t be used as an object-oriented array replacement (that would be pretty sweet, really), but given that in its state, the ArrayObject is really only useful for exposing lists from classes that won&#8217;t likely be manipulated extensively, its behavior suffices for the purpose. It&#8217;s still a good step for a more object oriented PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sunstormlabs.net/blog/2008/02/29/what-good-are-arrayobjects/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
