StarTutorial has continued their "Modern PHP Developer" series of tutorials with their latest covering the use of Iterators for working with sets of data.
If you have used a for loop in PHP, the idea of iteration is most likely not foreign to you. You pass an array to a for loop, and perform some logic inside the loop, but did you know that you can actually pass data structures other than arrays to a for loop? That's where Iterator comes into play.
The tutorial starts by introducing some of the basic concepts of what iterators are and how they're represented in PHP in their most basic form: arrays. They cover some of the basic array handing and functions before getting into the actual
Iterator object handling. The article is then broken up into a few parts covering iterators and their functionality:
- Your first iterator class
- Why iterator?
- SPL Iterators
- ArrayObject vs SPL ArrayIterator
- Iterating the File System
- Peeking ahead with CachingIterator
Code and a summary of the functionality is included in each section providing you with a great start towards using iterators over simple arrays in your modern PHP applications.