Re: Re: [PHP5]__get, __set and isset()

From: Date: Tue, 31 Aug 2004 17:51:20 +0000
Subject: Re: Re: [PHP5]__get, __set and isset()
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-195310@lists.php.net to get a copy of this message
Catalin Trifu wrote:
        Actually i think no property is created; not in the sense of
real object property.
    The only "real" property of the object is $elem.
    The $o->a, and $o->b are, IMHO, not object properties;
It is true that one can access $o->a and $o->b, but what is actually returned is a member of the array $elem, and only because of the implementation of the magic functions __set and __get.
    isset() does test for an "actual" variable beeing set, which is definetely
not the case.
    I agree however that this is confusing. I am definetely puzzled by the
usefullness of these methods since i consider this to be a real error trap; it would be so easy to fill up an object with values without knowing what they are and what they are supposed to do and when developing some kinda library, things can get really messy.
    On my part I would stick with the usual declaration of properties and not
use __set() and __get().
    I think it's much cleaner to say:
class OO {
    /** this is variable a and means ... */
    public $a;
    /** this is variable a and means ... */
    public $b;
} cheers, Catalin ""Frédéric hardy"" <f.hardy@hexanet.fr> wrote in message news:4134AB04.5060609@hexanet.fr...
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:
       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
-- ===================================================================
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 ===================================================================
I personally find the __get() and __set() methods very usefull, especially when constructing object hierarchies. But, as Frédéric said, they're not sufficient. Either make isset() work with the __get() method or add a __isset() magic method. -- Daniel Schierbeck

« previous php.general (#195310) next »