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.
Private the main character of today's "PHP Design Patterns" series 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 the 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.
http://www.bkjia.com/PHPjc/478776.html www.bkjia.com true http://www.bkjia.com/PHPjc/478776.html techarticle 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 cou ...