Using iterators
An iterator is a special type of class that allows you to traverse a container or list. The keyword here is traverse. What this means is that the iterator provides the means to go through a list, but it does not perform the traversal itself.
The SPL provides a rich assortment of generic and specialized iterators designed for different contexts. The ArrayIterator
, for example, is designed to allow object-oriented traversal of arrays. The DirectoryIterator
is designed for filesystem scanning.
Certain SPL iterators are designed to work with others, and add value. Examples include FilterIterator
and LimitIterator
. The former gives you the ability to remove unwanted values from the parent iterator. The latter provides a pagination capability whereby you can designate how many items to traverse along with an offset that determines where to start.
Finally, there are a series of recursive iterators, which allow you to repeatedly call the parent iterator. An example would be RecursiveDirectoryIterator...