next time, please read the isset documentation carefully. I quote:Next time, please read my post carefully. $a is not NULL, it is 1. Therefore, isset($a) should return TRUE, which it does normally, just not when $a is returned via __get().
[quote]isset() will return FALSE if testing a variable that has been set to NULL.[/quote]
Catalin Trifu wrote:
Hi,
Is this really a bug. I think not. There is no variable $o->a or $a->b in the class OO there is only the variable $elem and $a and $b is a member of that array So ... The fact that PHP5 provides __set and __get magic functions does not mean that the actual variables exist with that particular name in the class ? Perhaps it would be nice to have a __isset magic function ?
Cheers Catalin
<?php
class OO { private $elem = array("a" => 1);
public function __get ($prop) { if (isset($this->elem[$prop])) { return $this->elem[$prop]; } else { return NULL; } }
public function __set ($prop, $val) { $this->elem[$prop] = $val; } }
$o = new OO();
echo isset($o->a) ? "yes\n" : "no\n"; echo isset($o->b) ? "yes\n" : "no\n";
echo is_null($o->a) ? "yes\n" : "no\n"; echo is_null($o->b) ? "yes\n" : "no\n";
?>
I expected something like this:
yes no no yes
But got this:
no no no yes
It looks like isset() can't handle object properties correctly. I'll post the bug on php.net.
--
Daniel Schierbeck
@catalin: Yes, a __isset() method would be ideal, but i still can't see why isset() shouldn't support the overloaded variables - after all, if you are able to access such a variable using the __get() method, why shouldn't you be able to check whether or not it has been set? Currently, if you want to check whether a variable in the $elem array is set, you would have to use this:
$a = $o->a; echo isset($a) ? "yes\n" : "no\n";
Which in my opinion isn't optimal.
Bottom line - if you can access a variable through property overloading you should be able to use isset() on them as well.
-- Daniel Schierbeck
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php