[php-src] Issue #7952: `CallbackFilterIterator` is not rewound by default/initialized
| From: | damianwadley | Date: | Mon, 17 Jan 2022 04:46:14 +0000 |
| Subject: | [php-src] Issue #7952: `CallbackFilterIterator` is not rewound by default/initialized | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239043@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7952
Comment Author: damianwadley
This is correct as far as I know: iterators do not promise that they are rewound and valid when
constructed, and are generally in an undefined state (though individual iterator classes may decide
otherwise). If they did, ones that invoke code like CallbackIterator or Generator would have to
immediately execute on construction. ArrayIterator doesn't have side effects like that so it
could start in a valid state, but it could just as easily have not been implemented that way.
Standard pattern has always been that if you want to use an iterator then you use it the same way
that foreach does: rewind() to start, valid() to check the iterator is in a valid state, next() to
advance, and repeat until no longer valid.
How do you feel about making this a documentation bug? This "you are supposed to rewind before
using an iterator" issue has been raised before but AFAIK never really been spelled out in the
docs.