illustrate
1. Implementing the interface of other iterator functions is equivalent to installing a shell on other iterators. There is only one method.
2. Aggregation iterators can be combined with many iterators to achieve more efficient iteration.
Example
class MainIterator implements Iterator { private $var = array(); public function __construct($array) //Constructor function, initialize object array { if (is_array($array)) { $this->var = $array; } } public function rewind() { echo "rewindingn"; reset($this->var); //Point the internal pointer of the array to the first element } public function current() { $var = current($this->var); // Return the current value in the array echo "current: $varn"; return $var; } public function key() { $var = key($this->var); //Return the key name of the current unit pointed to by the internal pointer in the array echo "key: $varn"; return $var; } public function next() { $var = next($this->var); //Return the value of the next unit pointed to by the internal pointer of the array echo "next: $varn"; return $var; } public function valid() { return !is_null(key($this->var); //Determine whether the key of the current unit is empty } }
The above is an introduction to PHP aggregate iterators. I hope it will be helpful to everyone.