|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43936 __empty() magic method
Submitted: 2008-01-25 14:40 UTC Modified: 2016-03-20 17:36 UTC
Avg. Score:4.6 ± 1.0
Reproduced:21 of 22 (95.5%)
Same Version:6 (28.6%)
Same OS:13 (61.9%)
From: james dot laver at gmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 5.2.5 OS: GNU/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: james dot laver at gmail dot com
New email:
PHP Version: OS:


 [2008-01-25 14:40 UTC] james dot laver at gmail dot com
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:


__empty.txt (last revision 2011-08-24 00:43 UTC by chrisstocktonaz at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-30 11:51 UTC]
-Status: Open +Status: Assigned -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: dmitry
 [2010-06-30 11:51 UTC]
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.
 [2010-06-30 11:52 UTC]
-Package: *General Issues +Package: Class/Object related
 [2010-06-30 12:00 UTC] icy at lighttpd dot net
Simple test script to trigger the bug:


class Foo {
	public $vars;

	function Foo() {
		$this->vars['bar'] = 1;

	function __get($var) {
		return $this->vars[$var];

$foo = new Foo;

$baz = $foo->bar;

Expected result:
bool(false) bool(false) 

Actual result:
bool(true) bool(false)
 [2010-06-30 13:02 UTC]
-Type: Feature/Change Request +Type: Documentation Problem -Assigned To: dmitry +Assigned To: pajoye
 [2010-06-30 13:02 UTC]
Let document the __isset requirements better, when one relies on empty.
 [2010-10-22 15:50 UTC]
-Status: Assigned +Status: Open -Package: Class/Object related +Package: Documentation problem -Assigned To: pajoye +Assigned To:
 [2010-10-23 16:06 UTC]
Automatic comment from SVN on behalf of kalle
Log: Added a remark about overloaded properties and language constructs (Bug #43936)
 [2010-10-23 16:08 UTC]
-Type: Documentation Problem +Type: Feature/Change Request -Package: Documentation problem +Package: Class/Object related
 [2010-10-23 16:08 UTC]
Fixed the overloading property vs language construct issue in the documentation.

This feature request is still valid, so moving it to be one instead of a documentation issue
 [2011-08-24 00:40 UTC] chrisstocktonaz at gmail dot com
I made the start of a patch for this, but there is some issues that would need to 
be worked out with has_property calls, it seems oddly interfaced with by the 
various callers.
 [2012-07-20 13:44 UTC] thomas at sibben dot nl
The documentation states:

"empty() only checks variables as anything else will result in a parse error"

if 'everything else' is a __get() returning an array with a value in it, it returns TRUE,

Ubuntu 10.04 PHP 5.3.2-1ubuntu4.17
 [2012-07-20 13:53 UTC] thomas at sibben dot nl
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();
 [2016-03-20 17:36 UTC]
-Status: Open +Status: Wont fix
 [2016-03-20 17:36 UTC]
This functionality is already supported, you simply need to implement both __isset() and __get(). empty($foo->bar) is semantically identical to !isset($foo->bar) || !$foo->bar. As such, a meaningful handling of empty() requires both of these handlers to be implemented.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Jan 28 12:05:53 2023 UTC