Req #43909 [Com]: Support for a method method to determine the 'emptiness' of an object
| From: | robert dot johnson at icap dot com | Date: | Tue, 07 Jan 2014 15:25:48 +0000 |
| Subject: | Req #43909 [Com]: Support for a method method to determine the 'emptiness' of an object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-183626@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=43909&edit=1
ID: 43909
Comment by: robert dot johnson at icap dot com
Reported by: grant dot kidd at gmail dot com
Summary: Support for a method method to determine the
'emptiness' of an object
Status: Open
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: Linux
PHP Version: 5.2.5
Block user comment: N
Private report: N
New Comment:
I want this too.
Often I don't want to throw from the constructor, and there are many dynamic uses of such a
method, like the C++ operator!().
It also makes code look better and allows generic code to work across the board, whatever the type
of variable.
Previous Comments:
------------------------------------------------------------------------
[2008-01-22 17:16:41] grant dot kidd at gmail dot com
Description:
------------
I've noticed that there is no method for determining if an object is 'false' in the
same way that an empty array is equivalent to false. Although the countables interface would be
helpful for implementing a feature mimicing array, having a magic method '__isEmpty' would
be best for anyone who wants to control the 'emptiness' of an object.
This is a suggestion to implement a magic method to determine the true/false value of an object (in
the same way a string or int or array can be true or false)
Reproduce code:
---------------
class Something {
var $data;
public function __construct($data = null) {
if ($data) {
$this->data = $data;
}
}
public function __isEmpty() {
return isset($this->data);
}
}
$something = new Something();
if (!$something) {
echo "huzzah, the magic method works!"
}
Expected result:
----------------
huzzah, the magic method works!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=43909&edit=1