note 120999 added to class.iteratoriterator
| From: | thomas at gielfeldt dot dk | Date: | Thu, 20 Apr 2017 22:33:53 +0000 |
| Subject: | note 120999 added to class.iteratoriterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209033@lists.php.net to get a copy of this message | ||
IteratorIterator::current() does not invoke the inner iterator's current() method.
The inner iterator's current() method is invoked (and cached) on rewind() and next(). The same
goes for key() and valid().
Example:
<?php
$i = new ArrayIterator(range(1,10));
$i = new IteratorIterator($i);
iterator_to_array($i); // Seek to the end
print "Valid: " . $i->valid() . "\n";
$i->append('test');
print "Valid: " . $i->valid() . "\n";
print "Current: " . $i->current() . "\n";
?>
Output:
Valid:
Valid:
Current:
Whereas:
<?php
$i = new ArrayIterator(range(1,10));
iterator_to_array($i); // Seek to the end
print "Valid: " . $i->valid() . "\n";
$i->append('test');
print "Valid: " . $i->valid() . "\n";
print "Current: " . $i->current() . "\n";
?>
Output:
Valid:
Valid: 1
Current: test
The reason being the way current(), key() and valid() are invoked by the IteratorIterator as
mentioned above.
----
Server IP: 72.52.91.14
Probable Submitter: 87.60.150.15
----
Manual Page -- http://php.net/manual/en/class.iteratoriterator.php
Edit -- https://master.php.net/note/edit/120999
Del: integrated -- https://master.php.net/note/delete/120999/integrated
Del: useless -- https://master.php.net/note/delete/120999/useless
Del: bad code -- https://master.php.net/note/delete/120999/bad+code
Del: spam -- https://master.php.net/note/delete/120999/spam
Del: non-english -- https://master.php.net/note/delete/120999/non-english
Del: in docs -- https://master.php.net/note/delete/120999/in+docs
Del: other reasons-- https://master.php.net/note/delete/120999
Reject -- https://master.php.net/note/reject/120999
Search -- https://master.php.net/manage/user-notes.php