Take the State of PHP 2026 Survey →

Counting of non-countable objects View as Markdown

Implemented (PHP 7.2)
Authors
Craig Duncan php@duncanc.co.uk
Date
2016-10-04
Version
0.4
Source
wiki.php.net/rfc/counting_non_countables

Vote

Counting of non-countable objects closed
Yes
31
No
0
31 voters
ajf Yes 2016-10-23 15:28
alan_k Yes 2016-10-22 01:14
bishop Yes 2016-10-26 16:49
bwoebi Yes 2016-10-26 10:47
cmb Yes 2016-10-24 14:05
colinodell Yes 2016-10-21 18:47
danack Yes 2016-10-23 12:47
davey Yes 2016-10-26 06:18
galvao Yes 2016-11-01 02:54
guilhermeblanco Yes 2016-10-23 15:15
hywan Yes 2016-10-21 15:49
jhdxr Yes 2016-10-31 04:43
kguest Yes 2016-11-03 21:45
lcobucci Yes 2016-10-24 14:19
leigh Yes 2016-10-21 17:59
marcio Yes 2016-10-21 21:34
mariano Yes 2016-10-21 16:00
mbeccati Yes 2016-10-24 09:19
mfonda Yes 2016-10-24 15:41
mike Yes 2016-10-21 18:23
nikic Yes 2016-10-21 16:48
ocramius Yes 2016-10-24 09:35
omars Yes 2016-10-28 08:57
pierrick Yes 2016-11-04 18:56
rasmus Yes 2016-10-21 18:18
sammyk Yes 2016-11-02 14:41
santiagolizardo Yes 2016-10-31 14:32
tpunt Yes 2016-10-21 18:41
trowski Yes 2016-10-21 16:27
yohgaki Yes 2016-10-21 20:51
zimt Yes 2016-10-25 09:06

Discussions on the mailing list

[RFC] Counting of non-countable objects
by Craig Duncan · 13 messages
[RFC] [VOTE] Counting of non-countable objects
by Craig Duncan · 5 messages
Countable Type Hint
by Craig Duncan · 15 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Calling count() on a scalar or object that doesn't implement the Countable interface (http://php.net/manual/en/class.countable.php) returns 1. There are no known use cases for this, and it can easily hide bugs, take the following example:

function handle_records(iterable $iterable)
{
    if (count($iterable) === 0) {
        return handle_empty();
    }
 
    foreach ($iterable as $value) {
        handle_value($value);
    }
}

Passing a Generator that yields nothing would not call handle_empty(), or handle_value(), or alert the developer to the issue.

Proposal

This RFC proposes adding a warning when calling count() with a parameter that is a scalar, null, or an object that doesn't implement Countable.

Note that the sizeof alias is also affected. http://php.net/manual/en/function.sizeof.php

Backward Incompatible Changes

The call to count() will still return 1 (or 0 for null) so backwards compatibility is maintained.

Environments that display warnings or convert them to more severe errors/exceptions would be affected, but this should just bring attention to a bug in the code.

Proposed PHP Version(s)

PHP 7.2

Proposed Voting Choices

Simple Yes/No vote that doesn't change the language itself so requires 50% + 1 votes to get 'accepted'.

Vote

Vote: Counting of non-countable objects — Yes: 31 · No: 0 (closed)

Implementation

Pull request to handle the change: https://github.com/php/php-src/pull/2185

References

Initial discussion that led to this RFC's creation: https://github.com/php/php-src/pull/1672

Discussion thread: http://externals.io/thread/350

Voting thread: http://externals.io/thread/409