<?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>Wed, 22 May 2013 11:05:45 -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[PHPMaster.com: Using SPL Iterators, Part 2]]></title>
      <guid>http://www.phpdeveloper.org/news/17986</guid>
      <link>http://www.phpdeveloper.org/news/17986</link>
      <description><![CDATA[<p>
On PHPMaster.com today they've posted the <a href="http://phpmaster.com/using-spl-iterators-2/">second part of the series</a> covering the Iterators that come with PHP as a part of the <a href="http://php.net/spl">SPL</a>.
</p>
<blockquote>
In <a href="http://phpmaster.com/using-spl-iterators-1">part one</a> of this series I introduced you to some of the SPL Iterators and how to use them. There may be times however when the provided iterators are insufficient for your needs and you'll want to roll your own custom iterator. Luckily, SPL provides interfaces that will allow you to do just that. For an object to be traversable in a foreach loop, PHP requires that it be an instance of Traversable. You cannot however implement this interface directly (though you can use it in instaceof checks); instead you'll need to implement either SPL's Iterator or IteratorAggregate interfaces.
</blockquote>
<p>
He shows you how to implement these two interfaces in your own custom classes, looping through a set of books for the Iterator example and a "getIterator" method that creates an <a href="http://php.net/arrayiterator">ArrayIterator</a> when executed. The results of both are used in foreach loops showing how they can be used just like any other iteratable variables.
</p>]]></description>
      <pubDate>Tue, 22 May 2012 08:23:17 -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[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>
