php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30337 empty() does not return true on empty objects
Submitted: 2004-10-06 14:19 UTC Modified: 2004-10-28 11:21 UTC
Votes:7
Avg. Score:4.9 ± 0.3
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:1 (14.3%)
From: jorma dot tuomainen at softera dot fi Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.1 OS: Solaris
Private report: No CVE-ID: None
 [2004-10-06 14:19 UTC] jorma dot tuomainen at softera dot fi
Description:
------------
Empty() does not return true on empty objects. My 
understanding is that object that does not have methods or 
properties is empty(am I wrong?). 
 
Someone reported same thing on PHP 5 RC2 and it's still 
open w/o comments http://bugs.php.net/bug.php?id=28502 

Reproduce code:
---------------
<?php
class nuthin {
}
$bar=new nuthin;
$foo=new stdClass;
echo empty($bar);
echo empty($foo);
?>

Expected result:
----------------
11 

Actual result:
--------------
nothing aka empty page 

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-06 14:28 UTC] derick@php.net
Is this true Andi?
 [2004-10-06 22:05 UTC] andi@php.net
Does this also happen in zend.ze1_compatibility_mode? This was changed for non-compatibility mode with the change of moving objects to handles. As far as the new OO model is concerned objects are always handles, they are always true and they aren't empty. This is due to objects being an entity of their own now and not just a different syntax for accessing an array like structure. Just an example of why empty() might not make sense with objects is how you'd expect it to work with private/public/protected members.
 [2004-10-07 16:39 UTC] jorma dot tuomainen at softera dot fi
No, I'm not using compatibility mode. If this is wanted 
behaviour it should least be mentioned in the 
documentation. I still don't understand why empty() have 
to behave that way. Since that makes perfectly good 
function for a good purpose go to waste on objects. Is 
there going to be some function for checking empty 
objects(no methods or properties) or do I need to 
implement that myself?
 [2004-10-08 13:38 UTC] th at zirkonium dot net
I find this bug pretty irritating too. In my opinion 
empty() shouldn't behave this way, or at least like Jorma 
said, it should be mentioned in docs or some other way to 
check if objects are empty.
 [2004-10-11 20:48 UTC] andrey@php.net
<?php
function empty_object($obj){
  return count((array)$obj)==0;
}
class foo{var $a=1;}
$std=new stdclass;
$foo=new foo;
var_dump(empty_object($obj), empty_object($foo));
?>
bool(true)
bool(false)

 [2004-10-23 16:15 UTC] tony2001@php.net
"As far as the new OO model is concerned objects are always handles, they are always true and they aren't empty." (c) Andi

Making it a documentation problem then.
 [2004-10-25 08:39 UTC] aidan@php.net
It's covered under http://php.net/migration5

Perhaps this deserves a note on the empty() page, perhaps not.
 [2004-10-26 16:45 UTC] nlopess@php.net
I think that the empty() page deserves a little note about this.
Can you please add this, Aidan? :)
 [2004-10-28 11:21 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"In PHP 4 and earlier also objects with empty properties are considered empty while not in PHP 5."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 17:01:28 2024 UTC