title: php - multi-dimensional array wrapper class
body: <p>I have some data represented as multi-dimensional arrays. I would like to embed these data into my OO application and provide extra functionality to these data but also a way to access it both with the traditional random access and using an iterator (so I could use foreach). In other words <strong>I would like to implement a wrapping class for a multi-dimensional array</strong>.</p>
  
  <p>What I already tried:</p>
  
  <p><strong>I. ArrayAccess</strong>
  The problem with this one is that the server uses PHP 5.2.17 and <code>&amp;offsetGet()</code> gives an error, thus I can't return by reference, which means I can't implement multidimensional element access:</p>
  
  <pre><code>$obj[$key1][$key2] = $value;
  </code></pre>
  
  <p>Note, that upgrading to a newer PHP version is currently not an option</p>
  
  <p><strong>II. Using magic methods</strong>
  This is a bit trickier, but my problems rose when I tried using a variable as key. E.g.</p>
  
  <pre><code>$obj-&gt;$key1[$key2] = $value;
  </code></pre>
  
  <p>The interpreter first evaluated <code>$key1[$key2]</code> throwing a notice and returning the first char of $key1, and uses that as key on my array. I don't know how to solve this one either. I thought of using brackets to force operation priority, but that had the same problem as in my next attempt:</p>
  
  <p><strong>III. Using simple get and set functions</strong>
  Again, old PHP. It cries when I try to write:</p>
  
  <pre><code>$obj-&gt;get($key1)[$key2] = $value;
  </code></pre>
  
  <p>Because it doesn't know how to index an expression ending in round brackets.</p>
  
  <p>There's still the lost resort option: make the array public and forget OO all together. But before I go there I want to be certain that there's just really no other way of doing this.</p>