1. It is also an iterator itself. When it is instantiated, it needs to be passed to another iterator for internal storage, that is, the internal iteration InnerIterator.
2. The original iterator function can be upgraded by inheriting IteratorIterator.
Its construction parameters must also be iterators, and the parameter signature itself requires an object that implements the Traversable interface.
Example
class OutIterator extends IteratorIterator { public function rewind() { echo __METHOD__, PHP_EOL; return parent::rewind(); } public function valid() { echo __METHOD__, PHP_EOL; return parent::valid(); } public function current() { echo __METHOD__, PHP_EOL; return parent::current() . '_suffix'; } public function key() { echo __METHOD__, PHP_EOL; return parent::key(); } public function next() { echo __METHOD__, PHP_EOL; return parent::next(); } public function getInnerIterator() { echo __METHOD__, PHP_EOL; return parent::getInnerIterator(); } } $iterator = new OutIterator(new ArrayIterator([1, 2, 3])); foreach ($iterator as $k => $v) { echo $k, ': ', $v, PHP_EOL; } // OutIterator::rewind // OutIterator::valid // OutIterator::current // OutIterator::key // 0: 1_suffix // OutIterator::next // OutIterator::valid // OutIterator::current // OutIterator::key // 1: 2_suffix // OutIterator::next // OutIterator::valid // OutIterator::current // OutIterator::key // 2: 3_suffix // OutIterator::next // OutIterator::valid
The above is the understanding of PHP packaging iterator, I hope it will be helpful to everyone. More PHP learning guide: source code network