php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62155 empty () returns false for Countable objects with a count of 0
Submitted: 2012-05-25 10:16 UTC Modified: 2012-05-25 14:24 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: gordon dot mcvey at ntlworld dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.3.13 OS:
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gordon dot mcvey at ntlworld dot com
New email:
PHP Version: OS:

 

 [2012-05-25 10:16 UTC] gordon dot mcvey at ntlworld dot com
Description:
------------
If you call empty() on an array with 0 elements, then it will return true.  
However, if you call empty() on a Countable object with a count of 0, it returns 
false.  

This could catch out people who are implementing objects with Countable as array 
stand-ins. 

Test script:
---------------
<?php

class Test implements Countable
{
    public $count = 0;

    public function count ()
    {
        return intval (abs ($this -> count));
    }
}

$test = new Test ();

var_dump (empty ($test));
var_dump (count ($test));

$test -> count = 10;

var_dump (empty ($test));
var_dump (count ($test));

Expected result:
----------------
bool(true)
int(0)
bool(false)
int(10)

Actual result:
--------------
bool(false)
int(0)
bool(false)
int(10)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-25 14:24 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2012-05-25 14:24 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

empty: Determine whether a variable is considered to be empty.

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

http://www.php.net/manual/en/function.empty.php
 [2014-12-27 10:17 UTC] evought at pobox dot com
The response to this bug is mistaken. The behavior of empty in this case is according to the specification, but the specification contains an incurable inconsistency. Specifically, the documentation of ArrayAccess interface states:

"Interface to provide accessing objects as arrays."

However, the behavior of empty makes this a false statement. ArrayAccess implementors do not behave as arrays with respect to empty and there is no way within the specification to make them do so. One way or another, this is a "bug". Through the various SPL interfaces, Objects can be made to behave as arrays in other respects--- an explicit SPL design intention--- but not this one.

This would be merely a wart and easy to work around except for this critical fact noted by the original submitter:
"This could catch out people who are implementing objects with Countable as array stand-ins."

Given the misbehavior of empty in this respect, there is no way to substitute a 1st-class object for an array in an API without breaking client code. This leads to a fear of making that substitution and therefore to overuse of complex, fragile, multi-dimensional arrays when Objects are the correct implementation choice. The empty Object bug prevents obvious and otherwise compatible refactorings leading to enshrining bad design.

Personally, I find the exhortation to the original submitter to 'read the documentation' to be dismissive and insulting. It is quite clear that he had read the documentation and put work into writing a correct and complete report. That effort should not have been dismissed out-of-hand. There are arguably good reasons not to fix empty and array_key_exists, but the response does not present any such reasons.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 00:01:28 2024 UTC