|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43909 Support for a method method to determine the 'emptiness' of an object
Submitted: 2008-01-22 17:16 UTC Modified: 2021-02-15 13:16 UTC
Avg. Score:4.0 ± 0.9
Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%)
Same OS:2 (40.0%)
From: grant dot kidd at gmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: 5.2.5 OS: 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: grant dot kidd at gmail dot com
New email:
PHP Version: OS:


 [2008-01-22 17:16 UTC] grant dot kidd at gmail dot com
I've noticed that there is no method for determining if an object is 'false' in the same way that an empty array is equivalent to false.  Although the countables interface would be helpful for implementing a feature mimicing array, having a magic method '__isEmpty' would be best for anyone who wants to control the 'emptiness' of an object.

This is a suggestion to implement a magic method to determine the true/false value of an object (in the same way a string or int or array can be true or false)

Reproduce code:
class Something {

  var $data;

  public function __construct($data = null) {
    if ($data) {
      $this->data = $data;

  public function __isEmpty() {
    return isset($this->data);

$something = new Something();
if (!$something) {
  echo "huzzah, the magic method works!"

Expected result:
huzzah, the magic method works!


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-07 15:25 UTC] robert dot johnson at icap dot com
I want this too.  

Often I don't want to throw from the constructor, and there are many dynamic uses of such a method, like the C++ operator!().

It also makes code look better and allows generic code to work across the board, whatever the type of variable.
 [2017-07-24 13:57 UTC] jean dot claveau at c277 dot fr
An __isEmpty() magic method would also avoid loosing a lot of time with Iterables :
As they are used to replace arrays, it looks normal to use them like "if (!empty($myIterable)) ... " which will return false negatives really hard to debug.
 [2021-02-15 13:16 UTC]
-Status: Open +Status: Suspended -Package: Feature/Change Request +Package: *General Issues
 [2021-02-15 13:16 UTC]
If anybody is still interested in this feature, please pursue the
RFC process[1].  For the time being, I'm suspending this ticket.

[1] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Oct 26 09:03:38 2021 UTC