#44899 [Opn->Csd]: __isset usage changes behavior of empty()

From: Date: Sat, 03 May 2008 00:41:58 +0000
Subject: #44899 [Opn->Csd]: __isset usage changes behavior of empty()
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-780@lists.php.net to get a copy of this message
ID: 44899 Updated by: felipe@php.net Reported By: nuttzy at spellingcow dot com -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: CentOS PHP Version: 5.2.6 New Comment: 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) Previous Comments: ------------------------------------------------------------------------ [2008-05-02 20:53:35] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=44899&edit=1

« previous php.doc.bugs (#780) next »