__get() magic method and its use with empty() function.
| From: | Kuba Wieczorek | Date: | Sat, 04 Oct 2008 20:59:29 +0000 |
| Subject: | __get() magic method and its use with empty() function. | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-40860@lists.php.net to get a copy of this message | ||
Hello,
I'd like to talk about one thing I experienced using __get() magic method. It is a problem reported for the first time in a bug report (http://bugs.php.net/bug.php?id=24915) about five years ago but it has been closed and marked as 'Won't fix'.
Well, this is a sample code for this:
<?php
header('Content-type: text/plain');
class test
{
protected $a;
protected $b = 'test';
public function __get($var)
{
return $this->$var;
}
}
$test = new test;
print 'a: ' . var_export($test->a, true) . ' ' . var_export(empty($test->a), true) . "\n";
print 'b: ' . var_export($test->b, true) . ' ' . var_export(empty($test->b), true) . "\n";
?>
This is the current result:
a: NULL true
b: 'test' true
The expected one should be:
a: NULL true
b: 'test' false
There is a work-around for this, the vars have just to be assigned to another ones and then those ones checked with empty(), but of course it does not satisfy me.
The problem is really annoying. I am sure that there are many other developers here, who experienced it so I want to know your opinions about it.
Kuba Wieczorek