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 ...

Get Pro PHP: Patterns, Frameworks, Testing and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.