Why must I rewind IteratorIterator

前端 未结 3 1233
心在旅途
心在旅途 2020-12-21 17:01
$arrayIter = new ArrayIterator( array(1, 2) );
$iterIter = new IteratorIterator($arrayIter);

var_dump($iterIter->valid()); //false
var_dump($arrayIter->valid(         


        
3条回答
  •  清酒与你
    2020-12-21 17:16

    With a fresh iterator the position isn't initialized, simply for performance reason, you can stack iterators on top of other iterators, if all of them would rewind during construction there would be some performance impact, additionally some iterators might change their first value after the constructor was executed - which is unknown to iterators further out.

    Iterators are usually executed by foreach() which does a rewind() first ...

提交回复
热议问题