Re: [PHP] Re: [PHP5]__get, __set and isset()
I am agree with Catalin about no property is created, that __set() and __get() create virtual property. But, for the programmer, property which was set whith __set() and __get() ARE REAL, and must be use as real property. Conclusion : if php provide __set() and __get() to simulate object property, all function in the language must interact with them as usual, even if property are not real. So, an isset() call on a property wich was created witch __set() MUST return TRUE, and not FALSE. And __set and __get functions are very interesting. Imagine a session object, for example : class session { private $id = ''; private $data = array(); ... function __get($dataName) { if (isset($this-data[$dataName]) == true) return null; else return $this-data[$dataName]; } function __set($dataName, $dataValue) { $this-data[$dataName] = $dataValue; } } and you can do something like that : $session = new session(); if (isset($session-idUser) == true) $idUser = $session-idUser; else #http location on a login page And in login page : $session = new session(); $session-idUser = 'aUser'; But in this case, isset() MUST RETURN A GOOD RESULT, true Fred. Daniel Schierbeck wrote: 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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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
Re: [PHP] Re: [PHP5]__get, __set and isset()
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 ? __isset magic function would have to support also arrays: if(isset($a-foo['bar'])) So the function definition would have to be bool __isset(string varname [, mixed index [, mixed ...]]) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP5]__get, __set and isset()
Frédéric hardy wrote: Is it possible to do something like this : class foo { private $array = array(); function __construct() { ... } function __get($key) { return (isset($this-array[$key]) == false ? null : $this-array[$key]); } function __set($key, $value) { $this-array[$key] = $value; } } $foo = new foo(); if (isset($foo-bar) == false) $foo-bar = 'bar'; else echo $foo-bar; It seems that isset($foo-bar) return ALWAYS false. Bug ? Fred. It seems that you are right. I've tried off this code: ?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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [PHP5]__get, __set and isset()
Bug Id is 29917. Fred. Daniel Schierbeck wrote: Frédéric hardy wrote: Is it possible to do something like this : class foo { private $array = array(); function __construct() { ... } function __get($key) { return (isset($this-array[$key]) == false ? null : $this-array[$key]); } function __set($key, $value) { $this-array[$key] = $value; } } $foo = new foo(); if (isset($foo-bar) == false) $foo-bar = 'bar'; else echo $foo-bar; It seems that isset($foo-bar) return ALWAYS false. Bug ? Fred. It seems that you are right. I've tried off this code: ?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. -- === Frederic HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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 === -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP5]__get, __set and isset()
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP5]__get, __set and isset()
next time, please read the isset documentation carefully. I quote: [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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP5]__get, __set and isset()
M. Sokolewicz wrote: next time, please read the isset documentation carefully. I quote: [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 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(). @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
Re: [PHP] Re: [PHP5]__get, __set and isset()
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 HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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 === -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [PHP5]__get, __set and isset()
But there is no property set to NULL !! Fred. M. Sokolewicz wrote: next time, please read the isset documentation carefully. I quote: [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 -- === Frederic HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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 === -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [PHP5]__get, __set and isset()
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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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 === -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [PHP5]__get, __set and isset()
Frédéric hardy wrote: Is it possible to do something like this : class foo { private $array = array(); function __construct() { ... } function __get($key) { return (isset($this-array[$key]) == false ? null : $this-array[$key]); } function __set($key, $value) { $this-array[$key] = $value; } } $foo = new foo(); if (isset($foo-bar) == false) $foo-bar = 'bar'; else echo $foo-bar; It seems that isset($foo-bar) return ALWAYS false. Bug ? Not a bug, __get() and __set() are called only if !isset($foo-var). The purpose of __get and __set is to implement transparent getters and setters without forcing users to do function setVal($val) { $this-val = $val; } function getVal() { return $this-val; } If you are not sure which properties even exist, you probably shouldn't be using __get()/__set(), or should refactor so that you can rely upon certain values existing. If absolutely necessary, you can always provide function ifexists($name) { return isset($this-array[$name]); } and then you can if ($o-ifexists('a')) instead of if (isset($o-a)) Greg -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [PHP5]__get, __set and isset()
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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 HARDYEmail: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les CharmillesTel: +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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php