PHP empty() on __get accessor

fivefingerslc

New Member
Using PHP 5.3 I'm experiencing weird / non-intuitive behavior when applying \[code\]empty()\[/code\] to dynamic object properties fetched via the \[code\]__get()\[/code\] overload function. Consider the following code snippet:\[code\]<?phpclass Test { protected $_data= http://stackoverflow.com/questions/2045791/array('id'=> 23, 'name'=> 'my string' ); function __get($k) { return $this->_data[$k]; }}$test= new Test();var_dump("Accessing directly:");var_dump($test->name);var_dump($test->id);var_dump(empty($test->name));var_dump(empty($test->id));var_dump("Accessing after variable assignment:");$name= $test->name;$id= $test->id;var_dump($name);var_dump($id);var_dump(empty($name));var_dump(empty($id));?>\[/code\]The output of this function is as follow. Compare the results of the \[code\]empty()\[/code\] checks on the first and second result sets:Set #1, unexpected result:\[code\]string(19) "Accessing directly:"string(9) "my string"int(23)bool(true)bool(true)\[/code\]Expecting Set #1 to return the same as Set #2:\[code\]string(36) "Accessing after variable assignment:"string(9) "my string"int(23)bool(false)bool(false)\[/code\]This is really baffling and non-intuitive. The object properties output non-empty strings but \[code\]empty()\[/code\] considers them to be empty strings. What's going on here?
 
Back
Top