|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-07-20 19:34 UTC] benjcarson at digitaljunkies dot ca
[2005-03-06 20:49 UTC] sniper@php.net
[2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
[2006-07-16 20:48 UTC] info at peter-thomassen dot de
[2006-09-07 23:58 UTC] lf at burntmail dot com
[2006-11-13 11:04 UTC] phpbugs at thunder-2000 dot com
[2011-01-12 02:45 UTC] steven dot hartland at multiplay dot co dot uk
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 12:00:01 2025 UTC |
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