Original source: Blogspot
The main character of today's talk about PHP design patterns is the iterator (Iterator) pattern, which provides an abstraction in a very common process: an iteration on a set of objects (or scalars) in an unknown part of an object's 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. */class Collection implements Iterator { private $_content; Private $_index = 0; Public function __construct (array $content) { $this->_content = $content; } Public Function Rewind () { $this->_index = 0; } Public function valid () { 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 ($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 * is avail Able for implementation. */class Numbersset implements Iteratoraggregate { private $_content; Public function __construct (array $content) { $this->_content = $content; } Public function contains ($number) { return In_array ($number, $this->_content); } /** * Only this method is necessary to implement Iteratoraggregate. * @return Iterator * /Public Function getiterator () { return new Arrayiterator ($this->_content); } } echo "Numbersset:"; foreach ($array) as $key + $value) { echo "$key + $value.";} echo "\ n";
Let's play with recursiveiterator implementations $it = new Recursivearrayiterator (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->haschildr En ()? ' {Yes} ': ' {No} '; echo $children, '; } echo "\ n"; We can bridge it to a different contract via//a recursiveiteratoriterator, whose cryptic name//should is read as ' a n Iterator that spans over//a recursiveiterator '. echo "Foreach over a recursiveiteratoriterator:"; foreach (New Recursiveiteratoriterator ($it) as $value) { echo $value;} echo "\ n";
Iterator mode for PHP design patterns