<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>PHPDeveloper.org</title>
    <link>http://www.phpdeveloper.org</link>
    <description>Up-to-the Minute PHP News, views and community</description>
    <language>en-us</language>
    <pubDate>Sun, 19 May 2013 14:07:19 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Web Mozarts: Give the Traversable Interface Some Love]]></title>
      <guid>http://www.phpdeveloper.org/news/18580</guid>
      <link>http://www.phpdeveloper.org/news/18580</link>
      <description><![CDATA[<p>
In <a href="http://webmozarts.com/2012/10/07/give-the-traversable-interface-some-love/">this recent post</a> to the Web Mozarts site, <i>Bernhard Schussek</i> "gives Traversable some love" and introduces you to the <a href="http://php.net/manual/en/class.traversable.php">Traversable interface</a> and how it might work better for certain things than an Iterator.
</p>
<blockquote>
Let's start with a simple use case. Let's create an interface ArrayInterface that demarcates objects that behave like PHP arrays. The interface should allow for counting, iterating and array access.
</blockquote>
<p>
He shows how to create this interface based off of a "ArrayInterface" that implements "Countable", "ArrayAccess" and "Iterator" with all of the methods required for each. He points out that, while the documentation in <a href="http://php.net/manual/en/class.traversable.php">the manual</a> makes "Traversable" shouldn't be used, it can be extended instead of Iterator. This gives other classes that extend this interface the option of extending either of the Iterators ("Iterator" or "IteratorAggregate") they want.
</p>]]></description>
      <pubDate>Wed, 10 Oct 2012 09:12:56 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Jeremy Cook's Blog: Implementing IteratorAggregate and Iterator]]></title>
      <guid>http://www.phpdeveloper.org/news/17956</guid>
      <link>http://www.phpdeveloper.org/news/17956</link>
      <description><![CDATA[<p>
In a recent post to his blog <i>Jeremy Cook</i> has gotten back into looking at some of the SPL functionality that comes with PHP. In <a href="http://jeremycook.ca/2012/05/06/implementing-iteratoraggregate-and-iterator/">this new post</a> he looks specifically at the IteratorAggregate and Iterator object types.
</p>
<blockquote>
After a bit of a break I'm finally able to get back to writing about the predefined interfaces in PHP. PHP provides two interfaces that allow you to define how your objects behave in a foreach loop: IteratorAggregate and Iterator. Before taking a look at IteratorAggregate I'll briefly discuss how we can iterate over objects in PHP 'natively' and what it means to be Traversable.
</blockquote>
<p>
He introduces the concepts being being "iteratable" and "traversable". He then shows how to implement the IteratorAggregate (only one method required, "getIterator") and Iterator ("next", "valid", "current" and "key" methods required) in classes of your own. 
</p>
<p>
You can find out more about these two object types (including more sample usage) on their manual pages: <a href="http://us3.php.net/IteratorAggregate">IteratorAggregate</a> & <a href="http://us3.php.net/manual/en/class.iterator.php">Iterator</a>.
</p>]]></description>
      <pubDate>Mon, 14 May 2012 13:04:58 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Lorna Mitchell's Blog: Using iterator_to_array() in PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/17602</guid>
      <link>http://www.phpdeveloper.org/news/17602</link>
      <description><![CDATA[<p>
<i>Lorna Mitchell</i> has a new post to her blog today showing off a lesser-known but very useful function included in PHP - the <a href="http://www.lornajane.net/posts/2012/using-iterator_to_array-in-php">iterator_to_array</a> function, used to translate things that implement Traversable into arrays.
</p>
<blockquote>
Someone watching over my shoulder recently had never seen the ubiquitously-useful <a href="http://uk3.php.net/manual/en/function.iterator-to-array.php">iterator_to_array()</a> before. [...] Mostly I find this useful when I'm working with collections of data as these often present themselves as an object that you can foreach() over, but you can't dump it directly. If the object in question implements the <a href="http://uk3.php.net/manual/en/class.traversable.php">Traversable</a> interface, you can instead pass it into iterator_to_array to get the data as an array.
</blockquote>
<p>
She includes a brief snippet of code showing it in use - transforming the results from a MongoDB cursor object back into an array.
</p>]]></description>
      <pubDate>Wed, 29 Feb 2012 08:55:52 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Joshua Thijssen's Blog: SPL: Using the iteratorAggregate interface]]></title>
      <guid>http://www.phpdeveloper.org/news/17216</guid>
      <link>http://www.phpdeveloper.org/news/17216</link>
      <description><![CDATA[<p>
<i>Joshua Thijssen</i> has a <a href="http://www.adayinthelifeof.nl/2011/12/04/spl-using-the-iteratoraggregate-interface/">recent post</a> spotlighting a part of the <a href="http://php.net/spl">Standard PHP Library</a> (SPL) that implements that Traversable interface, the <a href="http://www.php.net/iteratorAggregate">IteratorAggregate</a> interface.
</p>
<blockquote>
Together with its more famous brother "Iterator", they are currently the two only implementations of the "Traversable" interface, which is needed for objects so they can be used within a standard foreach() loop. But why and when should we use the iteratorAggregate?
</blockquote>
<p>
He answers his question with an example - a book that contains chapters. With a normal iterator you'd have to define standard functions (like valid, rewind or key). Using the IteratorAggregate you can push items into an internal array (like chapters in a book) and call a "getIterator" method to get this set. He also takes it one step further and shows implementing the "Count" interface to make it easier to get a total count of the items in the iterator. Sample code is included to help clarify.
</p>]]></description>
      <pubDate>Tue, 06 Dec 2011 08:28:45 -0600</pubDate>
    </item>
    <item>
      <title><![CDATA[Fabien Potencier's Blog: Iterator or IteratorAggregate?]]></title>
      <guid>http://www.phpdeveloper.org/news/14705</guid>
      <link>http://www.phpdeveloper.org/news/14705</link>
      <description><![CDATA[<p>
Following up on two <a href="http://fabien.potencier.org/article/43/find-your-files">previous</a> <a href="http://fabien.potencier.org/article/45/iterator-or-iteratoraggregate">posts</a> about iterators, <i>Fabien Potencier</i> is back with one more quick shot on iterator aggregation - a look what using <a href="http://fabien.potencier.org/article/45/iterator-or-iteratoraggregate">iterator versus iteratoraggregate</a>.
</p>
<blockquote>
If you have ever used iterators in your code, you have probably implemented the Iterator interface. Objects of a class that implements Iterator  can be iterated over with the foreach loop. [...] The IteratorAggregate interface is quite similar [to Iterator] (both interfaces implement Traversable) but creates an external Iterator. But when the iterator is based on an array, creating an external Iterator for this array gives you a more concise and more readable code.
</blockquote>
<p>
His example code shows how, in implementing and IteratorAggregate, you can grab the instance of the Iterator even if it's based on an array.
</p>]]></description>
      <pubDate>Fri, 25 Jun 2010 12:09:44 -0500</pubDate>
    </item>
  </channel>
</rss>
