The protagonist of today's PHP design pattern series is the iterator (Iterator) pattern, which provides an abstraction: an iteration on a set of objects (or scalars) in an unknown part of the object graph.
Iterator (Iterator) mode, which provides an abstraction on a very common process: an iteration on a set of objects (or scalars) in an unknown part of an object graph. Iterations have several different methods of execution: Iterating over Array properties, collection objects, arrays, or even a query result set.
In the world of objects, the iterator pattern maintains an array-like function as a non-intrusive object facet (facet), and the client class is often separated from the real object implementation, referring to the iterator interface. Whenever possible, we can send a reference to an iterator instead of a specific or abstract class that might change in the future.
Figure 1 Iterator Mode
Participants:
Client: A method that references an iterator pattern executes a loop on a set of values or objects.
Iterator (Iterator): An abstraction in the iterative process, including methods such as Next (), isfinished (), current ().
Specific iterators (concreteiterators): Implement iterations on a specific set of objects, such as arrays, trees, combinations, collections, and so on.
With the Traversable interface, the PHP primitive supports the iterator pattern, which is extended by iterator and iteratoraggregate, which not only defines a standard set of methods, but also the two sub-interfaces. Each Traversable object can be passed intact to foreach (), which is the primary client of the iterator, the iterator implementation is a true iterator, and Iteratoraggregate is a traversable object with other responsibilities , which returns a iterator through the Getiterator () method.
The standard PHP Library is the only common purpose object-oriented library bound in PHP that defines additional interfaces and common classes. Outeriterator implementation decoration A iterator,cachingiterator and limititerator are two examples of this interface.
Recursiveiterator is an extension of the iterator interface to a tree structure that defines an additional set of methods to check for the existence of child objects of the current element in the iteration. Recursivearrayiterator and Recursivedirectoryiterator are examples of implementations of this interface, and these types of iterators can be used as-is. Or a recursiveiteratoriterator bridge to a common iterator contract. This outeriterator implementation will perform a depth-first or breadth-first traversal based on the construction parameters.
When using Recursiveiteratoriterator, you can pass it to foreach, see the following code examples for different uses of recursiveiterators and their superset iterator. Finally, Seekableiterators adds a seek () method to the contract that can be used to move the internal state of the iterator to a specific iteration point.
Note that iterators are a better abstraction than object sets, because we can let infiniteiterators,norewinditerators and so on, without being consistent with normal array arrays, so iterator lacks functions such as the count () function.
A complete list of SPL iterators can be found in the official PHP manual. Thanks to strong support for PHP, much of the work using the iterator pattern is included in the standard implementation, and the following code example takes advantage of the functionality of standard iterator and recursiveiterators.
<?PHP/** * Collection that wraps a numeric array. * All five public methods is needed to implement * the Iterator interface. */ classCollectionImplementsIterator {Private $_content; Private $_index= 0; Public function__construct (Array $content) { $this->_content =$content; } Public function Rewind() { $this->_index = 0; } Public functionvalid () {return isset($this->_content[$this-_index]); } Public function Current() { return $this->_content[$this-_index]; } Public function Key() { return $this-_index; } Public function Next() { $this->_index++; } } $arrayarray=Array(' A ', ' B ', ' C ', ' D '); Echo"Collection:"; foreach(NewCollection ($array) as $key=$value) { Echo"$key=$value. "; } Echo"\ n"; /** * Usually iteratoraggregate is the interface to implement. * It has only one method, which must return a Iterator * already defined as another class (e.g. Arrayiterator) * Iterator gives a finer control over the algorithm, * because all the hook points of Iterator ' contract * a Re available for implementation. */ classNumberssetImplementsIteratoraggregate {Private $_content; Public function__construct (Array $content) { $this->_content =$content; } Public functionContains$number) { return In_array($number,$this-_content); } /** * Only this method is necessary to implement Iteratoraggregate. * @return Iterator*/ Public functionGetiterator () {return NewArrayiterator ($this-_content); } } Echo"Numbersset:"; foreach(NewNumbersset ($array) as $key=$value) { Echo"$key=$value. "; } Echo"\ n"; //Let's play with Recursiveiterator implementations $it=NewRecursivearrayiterator (Array( ' A ', ' B ',Array( ' C ', ' D ' ),Array( Array( ' E ', ' F ' ),Array( ' G ', ' H ', ' I ' ) ) )); //$it is a recursiveiterator but also an Iterator,//So it loops normally over the four elements// of the array. Echo"Foreach over a recursiveiterator:"; foreach($it as $value) { Echo $value; //But recursiveiterators specify additional//methods to explore children nodes $children=$it->haschildren ()? ' {Yes} ': ' {No} '; Echo $children, ‘ ‘; } Echo"\ n"; //we can bridge it to a different contract via//a recursiveiteratoriterator, whose cryptic name// Should is read as ' an Iterator that spans over//a recursiveiterator '. Echo"Foreach over a recursiveiteratoriterator:"; foreach(NewRecursiveiteratoriterator ($it) as $value) { Echo $value; } Echo"\ n";
Reprinted from Http://developer.51cto.com/art/201004/197287_all.htm
PHP Design Pattern iterator mode