SPL迭代器的工作和代理模式OuterIterator
1.迭代器通过foreach语言结构来实现迭代,没有实现迭代器接口(Iterator)的普通对象使用foreach结构会遍历公共属性.
2.OuterIterator用于做为实际迭代器的代理(即代理模式)
3.OuterIterator有RecursiveIteratorIterator(平面化多维结构,数组等),LimitIterator,等
4.由于OuterIterator概念的代理性质,实际上可以将它们叠加在一起使用
例:
<?php $array = array( "Hello", // Level 1 array( "World" // Level 2 ), array( "How", // Level 2 array( "are", // Level 3 "you" // Level 3 ) ), "doing?" // Level 1 ); // Create our Recursive data structure $recursiveIterator = new RecursiveArrayIterator($array); // Create our recursive iterator $recursiveIteratorIterator = new RecursiveIteratorIterator($recursiveIterator); // Create a limit iterator $limitIterator = new LimitIterator($recursiveIteratorIterator, 2, 5); // Iterate foreach ($limitIterator as $key => $value) { $innerIterator = $limitIterator->getInnerIterator(); echo "Depth: " .$innerIterator->getDepth() . PHP_EOL; echo "Key: " .$key . PHP_EOL; echo "Value: " .$value . PHP_EOL; } ?>