|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-01-25 14:40 UTC] 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:
--------------
.....
Patches__empty.txt (last revision 2011-08-24 00:43 UTC by chrisstocktonaz at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 07:00:01 2025 UTC |
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)reproducable with: class Test { private $test; function __construct($test) { $this->test = $test; } public function __get($key) { return $this->$key; } public function getTest() { return $this->test; } } $test = new Test('test'); if (!empty($test->test)) { echo $test->test; } else { echo 'empty'; } echo '<br />'; echo $test->test; echo '<br />'; echo $test->getTest();