php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #40797 overloading with __isset() also affects empty()
Submitted: 2007-03-13 23:57 UTC Modified: 2007-03-23 14:24 UTC
From: stephan_schmidt at usa dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: Windows 200X
Private report: No CVE-ID: None
 [2007-03-13 23:57 UTC] stephan_schmidt at usa dot com
Description:
------------
When overloading with __get() to grab an index of an associative array inside an object, the overloaded variable returns 'false' when tested with empty() whether the value is empty or not (see Example 2 for incorrect results).

Testing overloaded variables that were not in an associative array with empty() returns the expected results (Example 3).

Reproduce code:
---------------
<?
class A { 
	public $myArray = array('nonEmptyVar' => 5, 'emptyVar' => '');
	function __get($var) {
		return $this->myArray[$var];
	}
}

class B {
	public $nonEmptyVar = 5;
	public $emptyVar = '';
	function __get($var) {
		return $this->$var;
	}
}

$a = new A();
$b = new B();

echo '<p>Example 1:<br>';
echo empty($a->myArray['nonEmptyVar']) ? 'empty' : 'not empty';
echo '<br>';
echo empty($a->myArray['emptyVar']) ? 'empty' : 'not empty';

echo '<p>Example 2:<br>';
var_dump($a->nonEmptyVar);
echo empty($a->nonEmptyVar) ? 'empty' : 'not empty';
echo '<br>';
var_dump($a->emptyVar);
echo empty($a->emptyVar) ? 'empty' : 'not empty';

echo '<p>Example 3:<br>';
echo empty($b->nonEmptyVar) ? 'empty' : 'not empty';
echo '<br>';
echo empty($b->emptyVar) ? 'empty' : 'not empty';
?>

Expected result:
----------------
Example 1:
not empty
empty

Example 2:
int(5) not empty
string(0) "" empty

Example 3:
not empty
empty

Actual result:
--------------
Example 1:
not empty
empty

Example 2:
int(5) empty
string(0) "" empty

Example 3:
not empty
empty

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-14 00:11 UTC] tony2001@php.net
That's exactly how it works here.
Make sure you've disable any zend_extensions.
 [2007-03-14 00:57 UTC] stephan_schmidt at usa dot com
The "Actual Results" were from the results of the above script run on a clean install of PHP 5.2.1 (Win 2000 SP4). No zend extensions are present or in use.

The code above did not run as expected on any of the Win 200X servers it was tested on. Line 6 of the output read "int(5) empty" in every test case.

I suspect it may have to do with the fact that empty() does not accept a function call as a parameter (i.e. empty(trim($myVar))) since overloading calls __get. However, one would expect it to also result in a fatal error if that were the case.
 [2007-03-14 01:36 UTC] tony2001@php.net
Dmitry, please take a look at zend_std_has_property().

 [2007-03-14 11:24 UTC] dmitry@php.net
To make isset() and empty() work with overloaded properties you must implement __isset() magic method, otherwise isset() and empty() check for regular properties.

<?php
class A { 
	public $myArray = array('nonEmptyVar' => 5, 'emptyVar' => '');
	function __get($var) {
		return $this->myArray[$var];
	}
	function __isset($var) {
		return isset($this->myArray[$var]);
	}
}

$a = new A();
echo empty($a->nonEmptyVar) ? 'empty' : 'not empty';
echo '<br>';
echo empty($a->emptyVar) ? 'empty' : 'not empty';
?>



 [2007-03-15 14:32 UTC] stephan_schmidt at usa dot com
Thanks. The fact that overloading with __isset() also affects empty() should be documented somewhere, as the page on overloading (http://us3.php.net/manual/en/language.oop5.overloading.php) contains only examples for isset().
 [2007-03-15 15:08 UTC] bjori@php.net
Re-opening & update summary

 [2007-03-23 14:24 UTC] vrana@php.net
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.

"Method __isset is called also with empy()."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC