note 119286 added to iteratoriterator.getinneriterator

From: Date: Wed, 04 May 2016 20:06:53 +0000
Subject: note 119286 added to iteratoriterator.getinneriterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-205427@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 72.52.91.14 Probable Submitter: 86.132.58.35 ---- Manual Page -- http://php.net/manual/en/iteratoriterator.getinneriterator.php Edit -- https://master.php.net/note/edit/119286 Del: integrated -- https://master.php.net/note/delete/119286/integrated Del: useless -- https://master.php.net/note/delete/119286/useless Del: bad code -- https://master.php.net/note/delete/119286/bad+code Del: spam -- https://master.php.net/note/delete/119286/spam Del: non-english -- https://master.php.net/note/delete/119286/non-english Del: in docs -- https://master.php.net/note/delete/119286/in+docs Del: other reasons-- https://master.php.net/note/delete/119286 Reject -- https://master.php.net/note/reject/119286 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205427) next »