HomePHP Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
Iterators in the Simplest Sense: Traversing Data Structures in PHP 5
Welcome to the final part of the series “Iterators in the Simplest Sense.” In this set of articles, you’ll learn the basic concepts of iterators in PHP 4 and PHP 5. The overall learning experience is strongly focused on the practical sense of the topic, so you can start quickly using iterators within your own PHP applications.
In order to remind you of some of the key concepts I explained in my previous tutorial of this series, let me mention briefly a few crucial points regarding iterators in PHP 4. As you'll probably recall, during the second article, I illustrated with a step-by-step approach how different data structures, specifically arrays, text files and MySQL result sets, can be easily traversed by the same set of methods, which were originally declared within a generic "Iterator" class, seated on top of the classes' hierarchy.
This approach allowed me to rapidly construct different iterator classes, which came in helpful for iterating over MySQL datasets and simple text files, utilizing the functionality provided by each of the methods defined within a specific array iterator class. As you hopefully learned from the hands-on examples you saw, once this class was created, building in distinct iterating classes is just a matter of deriving different subclasses from it, and specifically implementing the corresponding methods in consonance with the type of data being traversed.
Right, with reference to the content of the first two articles of this series, I think you'll have some nice material to keep you busy for long time coding iterators in PHP 4. Now, if you're one of the many developers working with PHP 5, things can be even more interesting, since PHP 5 offers native support for iterators through the SPL (Standard PHP Library).
Considering this propitious development scenario, in this last article I'll explore the implementation of PHP 5 iterators, by utilizing some of the embedded classes that integrate the powerful SPL package. Thus you can learn quickly how to include these programming structures inside your PHP applications.
After introducing the subject of this article, it's time to move forward, in order to start using iterators in PHP 5. Let's go!