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 HARDY                        Email: [EMAIL PROTECTED]
>>> 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.
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to