Popular PHP recipes tagged "oop"http://code.activestate.com/recipes/langs/php/tags/oop/2010-03-28T01:22:31-07:00ActiveState Code RecipesPHP 'Struct' port (PHP)
2010-03-28T01:22:31-07:00Jeff Griffithshttp://code.activestate.com/recipes/users/835605/http://code.activestate.com/recipes/577160-php-struct-port/
<p style="color: grey">
PHP
recipe 577160
by <a href="/recipes/users/835605/">Jeff Griffiths</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>In Ruby, the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct class</a> is a convenient way to create a hash-like object on the fly and use it for your nefarious purposes. PHP 5+ can be convinced to do this type of things as well, it just doesn't have it out of the box. Here is a simple class that implements iterator and allows you to populate the internal data structure similar to how Ruby's Struct works. Syntactic sugar? Probably.</p>
<p>Note: I haven't bothered to implement the Ruby Struct API per se, Instead I just got something similar by implementing the Iterator interface and keeping things very PHP-like.</p>