|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50981 CachingIterator::hasNext() appears to return incorrect values in some cases
Submitted: 2010-02-09 23:05 UTC Modified: 2018-08-18 11:22 UTC
Avg. Score:4.0 ± 0.8
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: clarity1285 at gmail dot com Assigned:
Status: Verified Package: SPL related
PHP Version: 5.3.1 OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: clarity1285 at gmail dot com
New email:
PHP Version: OS:


 [2010-02-09 23:05 UTC] clarity1285 at gmail dot com
When there are 11 total items and you use a LimitIterator to get the 
first 10, CachingIterator::hasNext() returns false even though there are 
more than 10 items in the initial set.

If there are 12 total items it works as expected.

Reproduce code:
$items = new ArrayObject(range(1,11));

echo 'there are ' . $items->count() . ' total items' . "\r\n";

$cachingIterator = new CachingIterator($items->getIterator());

$limitIterator = new LimitIterator($cachingIterator, 0, 10);

$i = 0;
foreach ($limitIterator as $item) {

echo 'first page has ' . $i . ' items' . "\r\n";

if ($cachingIterator->hasNext()) {
    echo 'there is a next page';
} else {
    echo 'there is no next page';

Expected result:
The code should output:

there are 11 total items 
first page has 10 items 
there is a next page

Actual result:
The code outputs:

there are 11 total items 
first page has 10 items 
there is no next page


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-12 17:04 UTC] aleksey dot v dot korzun at gmail dot com
I'm experiencing the same issue.
 [2010-02-12 17:23 UTC]
Please try using this snapshot:
For Windows:

 [2010-02-12 18:49 UTC] clarity1285 at gmail dot com
Tested with PHP 5.3.3-dev snapshot provided, and the issue is still 
 [2010-02-20 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2011-11-16 13:54 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: colder
 [2017-10-24 07:25 UTC]
-Status: Assigned +Status: Open -Assigned To: colder +Assigned To:
 [2018-08-18 11:22 UTC]
-Summary: CachingIterator::hasNext() does not return correct value in some cases +Summary: CachingIterator::hasNext() appears to return incorrect values in some cases -Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2018-08-18 11:22 UTC]
CachingIterator::hasNext() returns the correct result in this
case, since the inner iterator has no valid next element anymore.
See the slightly modified script[1].

This expected, but not explicitly documented behavior stems from
the fact that a CachingIterator is always one element ahead, which
it already caches.  For details see the explanation in my comment
on bug #49607.

Fixing the supplied test script is as simple as calling
CachingIterator::valid() instead of CachingIterator::hasNext().

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 13 08:01:30 2024 UTC