php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29234 empty($object->property) incorrect when property has access overloaded (__get)
Submitted: 2004-07-18 01:14 UTC Modified: 2005-03-14 01:00 UTC
Votes:11
Avg. Score:4.5 ± 0.8
Reproduced:10 of 10 (100.0%)
Same Version:3 (30.0%)
Same OS:2 (20.0%)
From: chrissy at codegoat dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.0.0 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chrissy at codegoat dot com
New email:
PHP Version: OS:

 

 [2004-07-18 01:14 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-20 19:34 UTC] benjcarson at digitaljunkies dot ca
This may be related to bug #28176.
 [2005-03-06 20:49 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-03-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-07-16 20:48 UTC] info at peter-thomassen dot de
The problem still exists with 5.1.4.
 [2006-09-07 23:58 UTC] lf at burntmail dot com
The problem still exists with 5.1.5.
 [2006-11-13 11:04 UTC] phpbugs at thunder-2000 dot com
The problem still exists with 5.2.
 [2011-01-12 02:45 UTC] steven dot hartland at multiplay dot co dot uk
Still exists in 5.3 as well, cmon guys this bugs been open for 6 years!
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 14:01:27 2025 UTC