1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | <?php namespace young;class InnerIterator implements \Iterator{ private $dates; private $position; public function __construct($dates = []) { $this->dates = $dates; $this->position = 0; } public function rewind() { echo 'call ' . __METHOD__ . '<br>'; reset($this->dates); } public function valid() { echo 'call ' . __METHOD__ . '<br>'; if ($this->position >= count($this->dates)) { # code... return false; } return true; } public function current() { echo 'call ' . __METHOD__ . '<br>'; return $this->dates[$this->position]; } public function key() { echo 'call ' . __METHOD__ . '<br>'; return $this->position; } public function next() { echo 'call ' . __METHOD__ . '<br>'; ++$this->position; }}class OuterIterator extends \IteratorIterator{ function rewind() { echo __METHOD__ . '<br>'; return parent::rewind(); } function valid() { echo __METHOD__ . '<br>'; return parent::valid(); } function current() { echo __METHOD__ . '<br>'; return parent::current() . '_suffix'; } function key() { echo __METHOD__ . '<br>'; return parent::key() ; } function next() { echo __METHOD__ . '<br>'; return parent::next() ; } function getInnerIterator() { echo __METHOD__ . '<br>'; return parent::getInnerIterator(); }}$tmpArr = array( '2018-10-01', '2018-10-02', //'2018-10-03',);$inner = new InnerIterator($tmpArr);$outer = new OuterIterator($inner);foreach ($outer as $key => $value) { # code... echo $key , '=>' , $value . '<hr>';} |
1 | young\OuterIterator::rewind |
1 2 3 4 | call young\InnerIterator::rewind call young\InnerIterator::valid call young\InnerIterator::current call young\InnerIterator::key |
1 2 3 4 5 | young\OuterIterator::valid young\OuterIterator::current young\OuterIterator::key 0=>2018-10-01_suffix young\OuterIterator::next |
1 2 3 4 | call young\InnerIterator::next call young\InnerIterator::valid call young\InnerIterator::current call young\InnerIterator::key |
1 2 3 4 5 | young\OuterIterator::valid young\OuterIterator::current young\OuterIterator::key 1=>2018-10-02_suffix young\OuterIterator::next |
1 2 | call young\InnerIterator::next call young\InnerIterator::valid |
1 2 3 4 5 | young\OuterIterator::valid object(young\InnerIterator)#1 (2) { [“dates”:“young\InnerIterator”:private]=> array(2) { [0]=> string(10) “2018-10-01” [1]=> string(10) “2018-10-02” } [“position”:“young\InnerIterator”:private]=> int(2) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //假如这里还是使用了上面的两个类代码 <?php namespace young; class InnerIterator implements \Iterator { //code 这里的代码假如和上面的一样 } class OuterIterator extends \IteratorIterator { //code 这里的代码假如和上面的一样 } $outer->valid(); //false $outer->current(); // _suffix 问题一 $outer->rewind(); $outer->valid(); //true $outer->current(); //2018-10-01_suffix $outer->next() $outer->rewind(); $outer->current(); //2018-10-02_suffix 问题二 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |