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 live online training, plus books, videos, and digital content from nearly 200 publishers.