|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44899 __isset usage changes behavior of empty()
Submitted: 2008-05-02 20:53 UTC Modified: 2008-05-03 01:29 UTC
From: nuttzy at spellingcow dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.6 OS: CentOS
Private report: No CVE-ID: None
 [2008-05-02 20:53 UTC] nuttzy at spellingcow dot com
Per, the behavior of empty() was modified to call the __isset magic method.  There is at least one case where this changes the behavior of empty() to falsely report "not empty" when in fact the desired result is "empty".

I suppose you would need an __empty() magic method, but that sounds heinous.  Overloading docs should at least be updated to warn the developer.

Reproduce code:
class myclass
    private $_raw_data = array() ;

    function __construct( $data)
        $this->_raw_data = $data ;

    function __isset( $field_name)
        return isset($this->_raw_data[$field_name]);

$_POST['foo']='' ;
$myclass = new myclass( $_POST) ;

// correct reports "isset"
echo (isset($myclass->foo)) ? 'isset' : 'not isset') ;

// incorrectly reports "not empty"
echo (empty($myclass->foo)) ? 'empty' : 'not empty' ;

// a way to get around the broken empty() call
echo ($myclass->foo) ? 'not empty' : 'empty' ;


Expected result:

Actual result:
not empty


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-03 00:41 UTC]
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.

Fixed in 5_3 and HEAD. (patch by Etienne Kneuss)
 [2008-05-03 01:29 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.


The source has been fixed, not the documentation.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Mar 07 04:01:23 2021 UTC