IteratorIterator::getInnerIterator

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

IteratorIterator::getInnerIteratorRetourne l'itérateur interne

Description

public IteratorIterator::getInnerIterator(): ?Iterator

Retourne l'itérateur interne.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

L'itérateur interne, fourni au constructeur IteratorIterator::__construct(), ou null s'il n'y a pas d'itérateur interne.

Voir aussi

add a note add a note

User Contributed Notes 1 note

up
0
c dot 1 at smithies dot org
7 years ago
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
?>
To Top