When PHP 5 arrived there was great excitement and rejoicing over the new object model. For many of us who struggled through PHP 4′s "Object Based" paradigm, PHP 5 was a ray of sunshine. We were so excited that many of us lost sight of another important addition to PHP 5, the Standard PHP Library (SPL).

Since then, the excitement over the object model has died down. Many authors and speakers have talked about, blogged about, and written about the SPL. However the SPL itself is a very large topic. We are going to narrow our focus down a bit to a subtopic of a specific section of the SPL. This blog post will deal with the OuterIterator. This is an interface defined in the SPL and used by several of the built in iterators.

He goes on to give a brief introduction about what the OuterIterator is and some examples uses for it, including one of the easier to follow - using it with a FilterIterator-based class. He gets into more detail about how the OuterIterator works and includes example snippets to help clarify. His "DwarfPrinter" class extends the FilterIterator and echoes out the names of The Seven Dwarves. He also includes an example of the output showing a more formatted output that follows the nesting and execution of the iterator. He finishes off the post with a list of iterators are useful to pair with the OuterIteator.

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?

He includes a bit of code showing the typical looping approach that a lot of developers take and how, using a FilterIterator, you can extend the default and make a custom "accept" method to remove certain matching items from the data set.

The SplFixedArray class provides a super-fast, fixed size array implementation. There are some limitations however, first you must use numeric keys and secondly you cannot use anonymous assignment (i.e. $array[] = 'value';). You'll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it's actually not a requirement that the size be fixed.

Because of these restrictions, the SplFixedArray is faster than its cousin - between 20 and 40 percent faster, depending on the size of the array. He includes a few snippets in the the post - one showing how he benchmarked the differences against simple arrays and another showing a more advanced example with another SPL type, a FilterIterator.

New on his blog Matthew Weier O'Phinney has this post looking about using the FilterIterator (from PHP's SPL libraries) to work with (recursive) directory iteration.

I'm currently doing research and prototyping for autoloading alternatives in Zend Framework 2.0. One approach I'm looking at involves creating explicit class/file maps; these tend to be much faster than using the include_path, but do require some additional setup. [...] I'm well aware of RecursiveDirectoryIterator, and planned to use that. However, I also had heard of FilterIterator, and wondered if I could tie that in somehow. In the end, I could, but the solution was non-obvious.

He starts with what he thought he should be able to do with the FilterIterator - pass in a DirectoryIterator to be able to filter them recursively. Unfortunately this only worked for the first level, so he looked else where. His solution ultimately involved passing in a RecursiveIteratorIterator instance into the DirectoryIterator that contained his RecursiveDirectoryIterator. He includes a full code example in the post showing how to locate a certain file/class recursively inside a directory.

On the PHPro.org website Kevin Waterson has posted a look at the AppendIterator iterator, a part of the SPL (Standard PHP Library) that is included with PHP5.

The SPL AppendIterator provides a method of iterating over multiple arrays at the same time. This can be very useful when aggregating data from different sources, such as XML or RSS feeds or even multiple database sources. The flexibility and efficiency of the whole SPL suite of tools is a the disposal of the AppendIterator making manipulating data in multiple objects fast and simple with a minimum of code.

His example shows what an AppendIterator object looks like, how to use it to add new values to an array and how to filter values out of the append process with a FilterIterator object.