From:             chrissy at codegoat dot com
Operating system: Windows XP
PHP version:      5.0.0
PHP Bug Type:     Unknown/Other Function
Bug description:  empty($object->property) incorrect when property has access 
overloaded (__get)

Description:
------------
The code below has a class with two properties.  One which is a regular
public class property and the other which is accessed through the __get
function.  Both are set to "Not Empty".  However, when you call empty() on
the one accessed through __get, the empty() function returns TRUE which is
incorrect.  The problem can be remedied by first assigning the value of
the property to a variable and then calling the empty function on that
variable.

Reproduce code:
---------------
<?php
class EmptyTest {
        public $emptyTest1 = "Not Empty";
        protected $properties = array ('emptyTest2' => "Not Empty");
        function __get($key) {
                if (array_key_exists($key, $this->properties)) return
$this->properties[$key];
        }
}
$emptyTest = new EmptyTest();
echo "The value of Test 1 is: \"" . $emptyTest->emptyTest1 . "\"<br/>The
value of Test 2 is: \"" . $emptyTest->emptyTest2 .
"\"<br/>-----------------------------------------------<br/><br/>";
if (empty($emptyTest->emptyTest1)) echo "Test 1 was empty <br/>";
else echo "Test 1 was not empty <br/>";
if (empty($emptyTest->emptyTest2))echo "Test 2 was empty <br/>";
else echo "Test 2 was not empty <br/>";
$test = $emptyTest->emptyTest2;
if (empty($test))echo "Test 2 was empty this time<br/>";
else echo "Test 2 was not empty this time<br/>";
?>

Expected result:
----------------
Both emptyTest1 and emptyTest2, when passed to the empty function, the
function should return true.

It could be that calling empty with a property that has had its access
overloaded by the __get function is invalid. If this is the case, I would
assume empty should at least throw a Warning.

Actual result:
--------------
The output of the above program is...

The value of Test 1 is: "Not Empty"
The value of Test 2 is: "Not Empty"
-----------------------------------------------

Test 1 was not empty
Test 2 was empty
Test 2 was not empty this time

-- 
Edit bug report at http://bugs.php.net/?id=29234&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=29234&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=29234&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=29234&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=29234&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=29234&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=29234&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=29234&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=29234&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=29234&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=29234&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=29234&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=29234&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29234&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=29234&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=29234&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=29234&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29234&r=float

Reply via email to