Countable interface prevents using 2nd parameter ($mode) of count() function

Bug #67064 Countable interface prevents using 2nd parameter ($mode) of count() function
Submitted: 2014-04-13 17:09 UTC Modified: 2014-08-04 08:35 UTC
From: trainmaster at gmx dot net Assigned:
Status: Closed Package: SPL related
PHP Version: Irrelevant OS: Windows 7 64 bit
Private report: No CVE-ID: None

 [2014-04-13 17:09 UTC] trainmaster at gmx dot net

Description:
------------
Implementing the countable interface does not allow to use the $mode parameter of the count() function.

I think it can be easily fixed without BC.

Test script:
---------------
class Counter implements Countable
{
    public function count()
    {
    	var_dump(func_get_arg(0));
    	return count($this->data);
    }
}

$counter = new Counter;
count($counter, COUNT_RECURSIVE);

Expected result:
----------------
// int(1)

Actual result:
--------------
// Warning: func_get_arg(): Argument 0 not passed to function

Patches

bug67064-BC (last revision 2014-06-13 13:53 UTC by mbeccati@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [2014-04-13 17:17 UTC] bwoebi@php.net

-Status: Open +Status: Assigned -Assigned To: +Assigned To: bwoebi

 [2014-04-13 17:17 UTC] bwoebi@php.net

When you implement an interface, you always can add an optional parameter:

    public function count($mode = COUNT_NORMAL) { ... }

should work too, when I'll fix this bug.

 [2014-04-13 17:24 UTC] bwoebi@php.net

-Status: Assigned +Status: Closed

 [2014-06-13 13:57 UTC] mbeccati@php.net

I've attached a patch that restores BC in the (very common) case when count() is called without arguments, so that Countable::count() is called rather than Countable::count(0).

It's not a big deal, but the BC break might break the application tests that expect a call without parameters. This would avoid having to do check for PHP 5.6 in such tests.

 [2014-06-13 16:42 UTC] bwoebi@php.net

I think this isn't preferable behavior. Maybe, with your patch BC is conserved in the most strict sense, but it's not the least confusing way when using this feature, I think.

 [2014-07-30 18:40 UTC] tyrael@php.net

-Status: Closed +Status: Re-Opened

 [2014-08-04 08:26 UTC] ab@php.net

-Status: Re-Opened +Status: Closed

 [2014-08-04 08:35 UTC] bwoebi@php.net

-Status: Closed +Status: Suspended -Assigned To: bwoebi +Assigned To:

 [2014-08-04 08:43 UTC] ab@php.net

-Status: Suspended +Status: Closed

 [2015-02-06 17:49 UTC] boen dot robot at gmail dot com

Will this change make it into PHP 7 at least?

(Because it's not currently, and the BC concerns don't exactly apply there...)