#40797 [Opn->Fbk]: empty() breaks when overloading with __get

From: Date: Wed, 14 Mar 2007 00:11:16 +0000
Subject: #40797 [Opn->Fbk]: empty() breaks when overloading with __get
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-110318@lists.php.net to get a copy of this message
ID: 40797 Updated by: tony2001@php.net Reported By: stephan_schmidt at usa dot com -Status: Open +Status: Feedback Bug Type: Class/Object related Operating System: Windows 200X PHP Version: 5.2.1 New Comment: That's exactly how it works here. Make sure you've disable any zend_extensions. Previous Comments: ------------------------------------------------------------------------ [2007-03-13 23:57:17] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=40797&edit=1

« previous php.bugs (#110318) next »