#44899 [NEW]: __isset usage changes behavior of empty()
| From: | nuttzy at spellingcow dot com | Date: | Fri, 02 May 2008 20:53:35 +0000 |
| Subject: | #44899 [NEW]: __isset usage changes behavior of empty() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-124677@lists.php.net to get a copy of this message | ||
From: nuttzy at spellingcow dot com
Operating system: CentOS
PHP version: 5.2.6
PHP Bug Type: Class/Object related
Bug description: __isset usage changes behavior of empty()
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
--
Edit bug report at http://bugs.php.net/?id=44899&edit=1
--
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=44899&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=44899&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=44899&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=44899&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=44899&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=44899&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=44899&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=44899&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=44899&r=support
Expected behavior: http://bugs.php.net/fix.php?id=44899&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=44899&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=44899&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=44899&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=44899&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=44899&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=44899&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=44899&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=44899&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=44899&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=44899&r=mysqlcfg