The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). PHP has built the Iterator interface since PHP5.3.
The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). Starting from PHP 5.3, PHP has built in the Iterator interface, implementing overlap for us.
The iterator mode provides an ordered access method for elements of an aggregate object, so that visitors do not have to worry about the internal implementation of the aggregate object. In fact, the most commonly used array foreach operation is to use the iterator (Note: The iterator is not equal to the iterator mode ). Starting from PHP 5.3, PHP has built the Iterator interface, which facilitates implementation of the Iterator mode:
// The Iterator interface provided by PHP5. The Traversable interface is only used to identify whether an object can be iterated through foreach. It does not contain any Iterator extends Traversable {/* Methods */abstract public mixed current (void) abstract public scalar key (void) abstract public void next (void) abstract public void rewind (void) abstract public boolean valid (void )}
Maybe you may find it strange (or redundant). For PHP, arrays themselves support iteration. Why do we need to encapsulate more layers through the iterator mode? This is the core of pattern definition: The visitor does not have to worry about the internal implementation of aggregate objects (such as the number of elements, KEY, and specific structure ); this means that the internal implementation of the aggregate object is not exposed to the visitor (for example, the object cannot be modified by the visitor ).
Implement our own iterator:
Class RecordIterator implements Iterator {private $ position = 0; // Note: The property of the object to be iterated is private $ records = array (); public function _ construct (Array $ records) {$ this-> position = 0; $ this-> records = $ records;} function rewind () {$ this-> position = 0;} function current () {return $ this-> records [$ this-> position];} function key () {return $ this-> position;} function next () {++ $ this-> position;} function valid () {return isset ($ this-> records [$ this-> position]);} // if we read the following data from MongoDB $ data = array (0 => array ('field' => 'value '), 1 => array ('field' => 'value'), 2 => array ('field' => 'value '), 3 => array ('field' => 'value'),); // use our own iterator $ records = new RecordIterator ($ data ); while ($ records-> valid () {print_r ($ records-> current (); $ records-> next () ;}$ records-> rewind ();
In the GoF-defined iterator mode, there are two participants: the container and the specific container, responsible for initializing and returning the specific iterator.
// Container interface Aggregate {public getIterator () ;}// the specific container class RecordList implements Aggregate {private $ iterator; public function _ construct ($ data) {$ this-> iterator = new RecordIterator ($ data);} public function getIterator () {return $ this-> iterator ;}} // use // if we obtain the following data from MongoDB $ data = array (0 => array ('field' => 'value '), 1 => array ('field' => 'value'), 2 => array ('field' => 'value '), 3 => array ('field' => 'value'),); $ recordList = new RecordList ($ data); $ iterator = $ recordList-> getIterator (); while ($ iterator-> valid () {print_r ($ iterator-> current (); $ iterator-> next ();}