Bug #62155 [NEW]: empty () returns false for Countable objects with a count of 0

From: Date: Fri, 25 May 2012 10:16:13 +0000
Subject: Bug #62155 [NEW]: empty () returns false for Countable objects with a count of 0
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170814@lists.php.net to get a copy of this message
From:             gordon dot mcvey at ntlworld dot com
Operating system: 
PHP version:      5.3.13
Package:          SPL related
Bug Type:         Bug
Bug description:empty () returns false for Countable objects with a count of 0

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 bug report at https://bugs.php.net/bug.php?id=62155&edit=1
-- 
Try a snapshot (PHP 5.4):            https://bugs.php.net/fix.php?id=62155&r=trysnapshot54
Try a snapshot (PHP 5.3):            https://bugs.php.net/fix.php?id=62155&r=trysnapshot53
Try a snapshot (trunk):              https://bugs.php.net/fix.php?id=62155&r=trysnapshottrunk
Fixed in SVN:                        https://bugs.php.net/fix.php?id=62155&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62155&r=needdocs
Fixed in release:                    https://bugs.php.net/fix.php?id=62155&r=alreadyfixed
Need backtrace:                      https://bugs.php.net/fix.php?id=62155&r=needtrace
Need Reproduce Script:               https://bugs.php.net/fix.php?id=62155&r=needscript
Try newer version:                   https://bugs.php.net/fix.php?id=62155&r=oldversion
Not developer issue:                 https://bugs.php.net/fix.php?id=62155&r=support
Expected behavior:                   https://bugs.php.net/fix.php?id=62155&r=notwrong
Not enough info:                     https://bugs.php.net/fix.php?id=62155&r=notenoughinfo
Submitted twice:                     https://bugs.php.net/fix.php?id=62155&r=submittedtwice
register_globals:                    https://bugs.php.net/fix.php?id=62155&r=globals
PHP 4 support discontinued:          https://bugs.php.net/fix.php?id=62155&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=62155&r=dst
IIS Stability:                       https://bugs.php.net/fix.php?id=62155&r=isapi
Install GNU Sed:                     https://bugs.php.net/fix.php?id=62155&r=gnused
Floating point limitations:          https://bugs.php.net/fix.php?id=62155&r=float
No Zend Extensions:                  https://bugs.php.net/fix.php?id=62155&r=nozend
MySQL Configuration Error:           https://bugs.php.net/fix.php?id=62155&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#170814) next »