Edit report at http://bugs.php.net/bug.php?id=43936&edit=1
ID: 43936 Comment by: icy at lighttpd dot net Reported by: james dot laver at gmail dot com Summary: __empty() magic method Status: Assigned Type: Feature/Change Request Package: Class/Object related Operating System: GNU/Linux PHP Version: 5.2.5 Assigned To: dmitry New Comment: Simple test script to trigger the bug: <?php class Foo { public $vars; function Foo() { $this->vars['bar'] = 1; } function __get($var) { return $this->vars[$var]; } } $foo = new Foo; var_dump(empty($foo->bar)); $baz = $foo->bar; var_dump(empty($baz)); Expected result: ---------------- bool(false) bool(false) Actual result: -------------- bool(true) bool(false) Previous Comments: ------------------------------------------------------------------------ [2010-06-30 11:51:54] paj...@php.net hi Dmitry, What's your thoughts on this one? Someone asked me about it and we found this old bug. A workaround is to implement __isset but that makes little sense (isset != empty) and is counter intuitive. ------------------------------------------------------------------------ [2008-01-25 14:40:45] james dot laver at gmail dot com Description: ------------ Using empty() on a variable that would be pulled from __get does not work, this has been established as 'not a bug' by more bug reports than anyone cares to remember, however, the documentation's suggestion of using __isset is not adequate if you wish to implement both isset and empty functionality. Thus I propose an __empty() magic method which would overload empty() in the way that __isset() overrides isset(). Reproduce code: --------------- public function __empty($value) { return empty($this->_values[$value]); } Expected result: ---------------- empty($object->my_empty_value) = true Actual result: -------------- ..... ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=43936&edit=1