Bug #76852 [Nab]: IteratorIterator doesn't initialize without being rewound

From: Date: Mon, 10 Sep 2018 19:18:02 +0000
Subject: Bug #76852 [Nab]: IteratorIterator doesn't initialize without being rewound
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216972@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76852&edit=1 ID: 76852 User updated by: morozov at tut dot by Reported by: morozov at tut dot by Summary: IteratorIterator doesn't initialize without being rewound Status: Not a bug Type: Bug Package: SPL related Operating System: Linux PHP Version: 7.2.9 Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [2018-09-08 23:48:05] requinix@php.net ArrayIterator may be usable immediately but that is not true for all iterators. IteratorIterator starts in an invalid state because the inner iterator may have as well. Always call rewind() before using *any* iterator. ------------------------------------------------------------------------ [2018-09-08 22:04:32] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76852&edit=1

« previous php.bugs (#216972) next »