php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
24 + 18 = ?
Subscribe to this entry?

 
 [2008-05-02 20:53 UTC] nuttzy at spellingcow dot com
Description:
------------
Per http://bugs.php.net/bug.php?id=40797, 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:
----------------
isset
empty
empty

Actual result:
--------------
isset
not empty
empty

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

Fixed in 5_3 and HEAD. (patch by Etienne Kneuss)
 [2008-05-03 01:29 UTC] felipe@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Reclassified...

The source has been fixed, not the documentation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 08:01:30 2024 UTC