CHAPTER 10SPL Iterators
As you learned in the previous chapter, in order to control looping with the foreach statement, all objects must implement the Traversable interface. But, the Traversable interface is an internal-only interface, so trying to implement it in your userland classes will simply result in an error. To actually implement looping of your objects, you must implement Traversable via the Iterator or IteratorAggregate interface.
For some uses, implementing Iterator might be enough, but when you need more functionality, you can choose from several extended iterator interfaces, which allow for seekable, recursive, aggregated, or even cached access.
Along with providing the capability to iterate objects, the SPL also contains a number ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access