php.net |  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
Votes:3
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: clarity1285 at gmail dot com
New email:
PHP Version: OS:

 

 [2010-02-09 23:05 UTC] clarity1285 at gmail dot com
Description:
------------
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) {
    ++$i;
}

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

Patches

Pull Requests

History

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] jani@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [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 
reproducible.
 [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] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: colder
 [2017-10-24 07:25 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: colder +Assigned To:
 [2018-08-18 11:22 UTC] cmb@php.net
-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] cmb@php.net
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] <https://3v4l.org/5JtJA>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 19:01:29 2024 UTC