php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43583 empty() fails when examining property of object accessed through __get
Submitted: 2007-12-12 19:07 UTC Modified: 2007-12-16 18:33 UTC
From: michael at dmgx dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michael at dmgx dot com
New email:
PHP Version: OS:

 

 [2007-12-12 19:07 UTC] michael at dmgx dot com
Description:
------------
Empty returns false whenever a property of an object is accessed through __get.  This problem has been reported before. The answer that "it's a feature" is entirely bogus - Either return the right value or throw a damn error but don't do nothing and leave people to wonder what the Hell is going on with their script. I just lost two full days to this "feature" and I'm not amused.

empty(someFunction());

throws an error.  If this bug is too much trouble to fix set it up so

empty($object->property);

also throws an error when a __get has been set.

Reproduce code:
---------------
class foo
{
	protected $data = array();
	public function __set($element, $value) { $this->data[$element] = $value; }
	public function __get($element) { return $this->data[$element]; }
}

$bar = new foo();

$bar->cat = 'meow';

empty($bar->cat); // returns false, as expected.

$bar->cat = '';

empty($bar->cat); // returns false, should return true.



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-12 21:50 UTC] felipe@php.net
In PHP 5.3 it works fine.
 [2007-12-13 04:23 UTC] crrodriguez at suse dot de
you have to define __isset magic method.
 [2007-12-16 18:33 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See also the previous comments. Everything else would be wrong.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 05:01:34 2025 UTC