IteratorIterator::getInnerIterator

(PHP 5 >= 5.1.0, PHP 7)

IteratorIterator::getInnerIteratorGet the inner iterator

说明

public Traversable IteratorIterator::getInnerIterator ( void )

Get the inner iterator.

参数

此函数没有参数。

返回值

The inner iterator as passed to IteratorIterator::__construct().

参见

User Contributed Notes

c dot 1 at smithies dot org 05-May-2016 05:06
The returned value from getInnerIterator() really is the inner iterator, not a clone. It should be used with respect: calling next() or rewind() on it, for example, will advance or reset the inner iterator - although the effect won't be noticed until you call next() on the IteratorIterator object - it seems as if it caches its current() and key() values (as of PHP v5.5.9). Even if the inner iterator itself is valid (i.e. valid() returns TRUE) the IteratorIterator won't report itself as valid until you either rewind it or call its next() method - these two methods cause the IteratorIterator to re-sync its current, key and valid states with the inner iterator.

<?php
$ii
= new IteratorIterator(new ArrayIterator(range(1,6)));
$i1 = $ii->getInnerIterator(); // gets the real thing
$i2 = $ii->getInnerIterator(); // ditto: $i2 === $i1 and the two are therefore in sync.
echo $i1->current(); // 1
echo $i1->key(); // 0
var_dump($ii->valid()); // FALSE
$i1->next(); // affects $i2, which is identical
echo $i1->key(); // 1
var_dump($ii->valid()); // still FALSE
$ii->rewind(); // rewinds $i1 and synchronizes
echo $ii->key(); // 0, as is $i1->key()
$i1->next(); // advances the inner iterator, which is now out of sync
echo $ii->key(); // still 0
echo $i1->key(); // 1
$ii->next(); // advances the inner iterator and syncs with it
echo $ii->key(); // 2
echo $i1->key(); // 2
?>