Bug #62155 [Opn->Nab]: empty () returns false for Countable objects with a count of 0
| From: | laruence@php.net | Date: | Fri, 25 May 2012 14:24:08 +0000 |
| Subject: | Bug #62155 [Opn->Nab]: empty () returns false for Countable objects with a count of 0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170826@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62155&edit=1
ID: 62155
Updated by: laruence@php.net
Reported by: gordon dot mcvey at ntlworld dot com
Summary: empty () returns false for Countable objects with a
count of 0
-Status: Open
+Status: Not a bug
Type: Bug
Package: SPL related
PHP Version: 5.3.13
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2012-05-25 10:16:13] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62155&edit=1