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

From: Date: Fri, 25 May 2012 11:35:17 +0000
Subject: Bug #62155 [Opn]: 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-170817@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 User updated by: gordon dot mcvey at ntlworld dot com Reported by: gordon dot mcvey at ntlworld dot com Summary: empty () returns false for Countable objects with a count of 0 Status: Open Type: Bug Package: SPL related PHP Version: 5.3.13 Block user comment: N Private report: N New Comment: m 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

« previous php.bugs (#170817) next »