Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Engine Yard Blog:
The Outer Iterator
Jan 13, 2014 @ 17:45:08

On the Engine Yard blog they have a new tutorial posted by Cal Evans looking at an object offered by the Standard PHP Library - the OuterIterator.

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.

tagged: outeriterator tutorial filteriterator standardphplibrary spl

Link: https://blog.engineyard.com/2014/the-outer-iterator

MaltBlue.com:
Painless Data Traversal with PHP FilterIterators
Oct 25, 2012 @ 13:54:35

On the MaltBlue blog Matt Setter has a new post introducing you to using FilterIterators for data traversal:

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.

tagged: filteriterator data traversal filter spl iterator array

Link:

Davey Shafik's Blog:
Faster Arrays
Nov 07, 2011 @ 14:54:58

In this new post to his blog Davey Shafik looks at an alternative to the traditional arrays most scripts use - something a little faster and more specific: SplFixedArray, part of the Standard PHP Library included with every release.

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.

tagged: spl splfixedarray array benchmark filteriterator

Link:

Matthew Weier O'Phinney's Blog:
Applying FilterIterator to Directory Iteration
Aug 17, 2010 @ 15:29:38

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.

tagged: iterator spl filteriterator directory tutorial

Link:

Matthew Turland's Blog:
A Few Kinks in FilterIterator
Aug 16, 2010 @ 15:18:40

In this quick post to his blog Matthew Turland shares a "kink" he found in using the FilterIteractor SPL iterator when working with the Phergie project's code.

Once I discovered the segfault [from the FilterIterator code], I had to come up with a short code sample exposing the bug in order to report it.

He talks about the bug that led him to the segfault and a second bug that was a side effect of the first causing the first element to be skipped during iteration.

tagged: filteriterator spl bug iterate segfault

Link:

PHPro.org:
SPL AppendIterator
Dec 17, 2008 @ 17:19:32

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.

tagged: tutorial spl appenditerator filteriterator filter array

Link:


Trending Topics: