ID: 29234
Comment by: benjcarson at digitaljunkies dot ca
Reported By: chrissy at codegoat dot com
Status: Open
Bug Type: Unknown/Other Function
Operating System: Windows XP
PHP Version: 5.0.0
New Comment:
This may be related to bug #28176.
Previous Comments:
------------------------------------------------------------------------
[2004-07-18 01:14:15] chrissy at codegoat dot com
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 this bug report at http://bugs.php.net/?id=29234&edit=1