<?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, 26 May 2013 03:49:14 -0500</pubDate>
    <ttl>30</ttl>
    <item>
      <title><![CDATA[Bob Majdak: Extending an Iterator to use an Iterator to make your code a little cleaner]]></title>
      <guid>http://www.phpdeveloper.org/news/19300</guid>
      <link>http://www.phpdeveloper.org/news/19300</link>
      <description><![CDATA[<p>
In <a href="http://catch404.net/2013/03/extending-an-iterator-to-use-an-iterator-to-make-your-code-a-little-cleaner/">this new post</a> to his site <i>Bob Majdak</i> talks about extending iterators to help make it easier to customize it for your needs.
</p>
<blockquote>
One of the nice things about iterators is the ability to shove them into other iterators, allowing you to wrap their functionality in other functionality to return a more precise result set. Take for example the idea that we want to read a directory to list only the images inside of it. There are two main ways to do this, via <a href="http://www.php.net/opendir>opendir</a> / <a href="http://www.php.net/readdir">readdir</a> functions and via <a href="http://www.php.net/FilesystemIterator">FilesystemIterator</a> objects. Going the FilesystemIterator route, one common way seems to be extend another class called <a href="http://www.php.net/FilterIterator">FilterIterator</a> which allows you to customize a filter based on you overwriting a method called accept().
</blockquote>
<p>
He shows not only overriding the "accept" method, but also the constructor to make using this new iterator a much simpler (and cleaner) call. You can find out more about the FilesystemIterator (and others) over in <a href="http://www.php.net/manual/en/spl.iterators.php">the Iterators section</a> of the PHP manual.
</p>]]></description>
      <pubDate>Tue, 12 Mar 2013 09:25:04 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[MaltBlue.com: Painless Data Traversal with PHP FilterIterators]]></title>
      <guid>http://www.phpdeveloper.org/news/18654</guid>
      <link>http://www.phpdeveloper.org/news/18654</link>
      <description><![CDATA[<p>
On the MaltBlue blog <i>Matt Setter</i> has a new post introducing you to <a href="http://www.maltblue.com/php/painless-data-traversal-with-php-filteriterators">using FilterIterators for data traversal</a>:
</p>
<blockquote>
There's load of ways to traverse data, especially in PHP where there are a variety of loops available; including while, do while, for and foreach. These are fine for normal structures, such as scalar and associative arrays. But what if you want to get a bit more fancy?
</blockquote>
<p>
He includes a bit of code showing the typical looping approach that a lot of developers take and how, using a <a href="http://php.net/filteriterator">FilterIterator</a>, you can extend the default and make a custom "accept" method to remove certain matching items from the data set.
</p>]]></description>
      <pubDate>Thu, 25 Oct 2012 08:54:35 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[PHPMaster.com: List Files and Directories with PHP]]></title>
      <guid>http://www.phpdeveloper.org/news/18644</guid>
      <link>http://www.phpdeveloper.org/news/18644</link>
      <description><![CDATA[<p>
On PHPMaster.com there's a new tutorial showing you how to <a href="http://phpmaster.com/list-files-and-directories-with-php/">work with files and directories</a> through your PHP applications. 
</p>
<blockquote>
In this article I'll talk about a common task you might have experienced while developing a PHP application: listing files and directories. I'll discuss several basic and advanced solutions, each having its pros and cons. First I'll present three approaches that use some very basic PHP functions and then progress to more robust ones which make use of SPL Iterators.
</blockquote>
<p>
The solutions they look at are the built-in functions like <a href="http://php.net/glob">glob</a> and <a href="http://php.net/readdir">readdir</a>/<a href="http://php.net/opendir">opendir</a> as well as SPL iterators up for the task - FilesystemIterator, RecursiveDirectoryIterator and GlobIterator. Code samples are included in the post, showing how to use each method to get and list the files. A few helpful hints are also included to finish off the tutorial (mostly about "tricks" to using the functions effectively).
</p>]]></description>
      <pubDate>Tue, 23 Oct 2012 08:56:25 -0500</pubDate>
    </item>
    <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[Thomas Weinart: What Iterators Can Do For You]]></title>
      <guid>http://www.phpdeveloper.org/news/18301</guid>
      <link>http://www.phpdeveloper.org/news/18301</link>
      <description><![CDATA[<p>
<i>Thomas Weinert</i> has a new post to his site showing some of the things that <a href="http://www.a-basketful-of-papayas.net/2012/07/what-iterators-can-do-for-you.html">iterators can do for you</a> (including working with arrays and aggregation).
</p>
<blockquote>
Basically Iterators provide a list interface for an object. Like all interfaces they are a contract how something can be used. If you use an interface it is not relevant how it is implemented - the implementation logic is encapsulated. It is of course relevant on the integration level. A bad implementation can impact the performance of you application. Even an good implementation may need special resources (like a database). But all this does not impact how you use it. Your code using the object with the Iterator interface stays the same.
</blockquote>
<p>
He shows how to use the <a href="http://php.net/IteratorAggregate"> IteratorAggregate</a>, <a href="http://php.net/ArrayIterator"> ArrayIterator</a>,  <a href="http://php.net/FilterIterator"> FilterIterator</a> and how to create a custom Iterator that you can extend in your own code.
</p>]]></description>
      <pubDate>Wed, 01 Aug 2012 09:55:22 -0500</pubDate>
    </item>
    <item>
      <title><![CDATA[Anthony Ferrara: What Generators Can Do For You]]></title>
      <guid>http://www.phpdeveloper.org/news/18263</guid>
      <link>http://www.phpdeveloper.org/news/18263</link>
      <description><![CDATA[<p>
<i>Anthony Ferarra</i> has a new post looking at <a href="http://blog.ircmaxell.com/2012/07/what-generators-can-do-for-you.html">using generators in your code</a> (as <a href="https://wiki.php.net/rfc/generators>proposed here</a>). He introduces the idea behind them and shows both a simple and more complex example of their use.
</p>
<blockquote>
The concept of generators was <a href="https://wiki.php.net/rfc/generators">recently proposed</a> for addition in PHP's core (Possibly for 5.5.0). While I believe that this is a great tool, it appears that many PHP developers aren't familiar with the concept of generators. So I thought I would take a little time and explain some of how it works, and how it can be used to greatly simplify code.
</blockquote>
<p>
He explains the concept of "generators" as an easier way to implement iterators. In his example he shows how to refactor is file handling iterator to replace it with generator functionality. It uses a new keyword, "yield", to return a Generator instance that can then can be used much like the file iterator without the need for all of the code to create the iterator itself. His more complex example shows how to replace an ArrayObject instance by a little trick inside its "getIterator" method.
</p>]]></description>
      <pubDate>Tue, 24 Jul 2012 08:43:58 -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[Stefan Koopmanschap's Blog: GlobIterator: Easy access to specific files]]></title>
      <guid>http://www.phpdeveloper.org/news/17828</guid>
      <link>http://www.phpdeveloper.org/news/17828</link>
      <description><![CDATA[<p>
<i>Stefan Koopmanschap</i> has a new post to his blog showing a <a href="http://www.leftontheweb.com/message/GlobIterator_Easy_access_to_specific_files">handy use of the GlobIterator</a> to access only certain files.
</p>
<blockquote>
For a project I am working on I needed to iterate over all .xml files in a specific directory. I started out with a DirectoryIterator, then considered I didn't want the XML filtering to take place inside my foreach loop. I decided to add a FilterIterator to the setup, but then felt this was not the right solution either. So I turned to my favorite SPL guru, <a href="http://adayinthelifeof.nl/">Joshua Thijssen</a>, to see if I was overseeing some kind of filter-option in the DirectoryIterator. I didn't, but I did oversee something else: <a href="http://php.net/globiterator">GlobIterator</a>.
</blockquote>
<p>
The GlobIterator lets you use functionality similar to the <a href="http://php.net/glob">glob</a> function (including being able to use wildcards in file searching) and get the resulting list back as a set of SplFileInfo objects, complete with additional metadata that can be extracted. 
</p>]]></description>
      <pubDate>Tue, 17 Apr 2012 12:43:42 -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>
  </channel>
</rss>
