Re: Re: [PHP5]__get, __set and isset()
| From: | Frédéric Hardy | Date: | Tue, 31 Aug 2004 16:44:52 +0000 |
| Subject: | Re: Re: [PHP5]__get, __set and isset() | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-195304@lists.php.net to get a copy of this message | ||
Yes.... but not :-).
With you use these magic functions, when you write $o->a = 'foo', you create a property in the object. Not ? Even if it is stored in an array member, __set() create a property and __get() retrieve its value.
Doc says :
The $name parameter used is the name of the variable that should be set or retrieved. The __set() method's $value parameter specifies the value that the object should set set the $name.Conclusion : class OO { private $elem = array(); 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"; # Must return false and return false effectively, cool $o->a = 'foo'; echo isset($o->a) ? "yes\n" : "no\n"; # Must return false and return false effectively, NOT COOL Fred. Catalin Trifu wrote:
-- =================================================================== Frederic HARDY Email: fch@hexanet.fr HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les Charmilles Tel: +33 (0)3 26 79 30 05 3, allée Thierry Sabine Direct: +33 (0)3 26 61 77 84 BP 202 - 51686 REIMS CEDEX 2 FRANCE ===================================================================Hi,Is this really a bug. I think not. There is no variable $o->a or $a->b in the class OOthere is only the variable $elem and $a and $b is a member of that arraySo ... The fact that PHP5 provides __set and __get magicfunctions 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