|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-09-08 22:04 UTC] morozov at tut dot by
Description: ------------ Right after construction, the state of IteratorIterator doesn't reflect the state of the underlying iterator. Despite the fact that the underlying iterator is valid and has non-empty values of key and value, the wrapper is invalid and hass NULL values of key and value. Test script: --------------- $it = new ArrayIterator([1, 2, 3]); var_dump($it->valid()); var_dump($it->key()); var_dump($it->current()); $it = new IteratorIterator($it); var_dump($it->valid()); var_dump($it->key()); var_dump($it->current()); Expected result: ---------------- bool(true) int(0) int(1) bool(true) int(0) int(1) Actual result: -------------- bool(true) int(0) int(1) bool(false) NULL NULL PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 11:00:02 2025 UTC |
Let's assume I have a component which instantiates the iterator and a few others which read data from it one by one. The readers, of course, cannot rewind it because it needs to remain in its current state, but why would the instantiator rewind a just created iterator? > IteratorIterator starts in an invalid state because the inner iterator may have as well. It's fine if it just reflects the state of the inner iterator, but if the inner iterator is valid, why is the wrapper not? If the implementation just proxied all calls to the underlying iterator, like: ``` function valid() { $this->innver->valid(); } ``` it wouldn't have this issue and would be the most simple implementation I can imagine. Why can't this be done?