-
Book Overview & Buying
-
Table Of Contents
Mastering PHP Design Patterns
By :
The Iterator design pattern is where an iterator is used to traverse a container. In PHP, a class is traversable using the foreach construct if it ultimately inherits the Traversable interface. Unfortunately, this is an abstract base interface, you can't implement it alone (unless you're writing in the PHP core itself). Instead, you must instead implement interfaces called Iterator or IteratorAggregate. By implementing either of these interfaces you make a class iterable and traversable using foreach.
Iterator and IteratorAggregate interfaces are very similar, except the IteratorAggregate interface creates an external iterator. IteratorAggregate as an interface only requires outlines one method, getIterator. This method has to return an instance of the ArrayIterator interface.
Let's suppose we want to create an implementation of this interface, which will iterate through various times.
Firstly, let's start off with a basic implementation of the IternatorAggregate...