|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60577 count(new EmptyIterator) returns 1
Submitted: 2011-12-20 20:17 UTC Modified: 2018-09-29 10:10 UTC
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: adam at sixohthree dot com Assigned: levim (profile)
Status: Closed Package: SPL related
PHP Version: 5.4SVN-2011-12-20 (SVN) OS:
Private report: No CVE-ID: None
 [2011-12-20 20:17 UTC] adam at sixohthree dot com
As an object that simulates a list of zero items, count(new EmptyIterator) should 
return zero.

Test script:
$it = new EmptyIterator;

Expected result:

Actual result:


bug60577_emptyiterator_count.diff (last revision 2011-12-20 20:20 UTC by adam at sixohthree dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-12 20:33 UTC]
-Status: Open +Status: Verified
 [2013-09-12 20:33 UTC]
I confirmed this for versions 5.1.0 - 5.5.3.
 [2013-09-12 20:37 UTC]
-Assigned To: +Assigned To: levim
 [2014-03-06 13:03 UTC] narf at devilix dot net
Technically not a duplicate, but very similar to #43650.
 [2014-05-09 08:16 UTC] iam4webwork at hotmail dot com
If this is a bug, then why is #46322 not a bug?
 [2014-05-09 14:13 UTC]
To be quite honest, this is the same exact 'bug'. The difference is really that some other Iterators implement Countable so you would expect an EmptyIterator to be Countable and return its count as 0.

The reason I haven't 'fixed' this bug yet is because there are fewer Iterators that implement Countable than I initially thought. I'm not quite sure what to do and have been working on other issues and features, notably adding return types:
 [2017-10-24 05:19 UTC]
-Status: Verified +Status: Assigned
 [2018-08-10 03:15 UTC]
In PHP 7.2 we've introduced a warning for passing uncountable things to `count`:

May we close this one?
 [2018-09-29 10:10 UTC]
-Status: Assigned +Status: Closed
 [2018-09-29 10:10 UTC]
Yes, I believe the warning is sufficient. Using count() on iterators will generally not produce any meaningful result, and the warning should make that obvious.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Aug 14 12:05:49 2022 UTC