Re: [PHP] overloading members. aghhh!!!

2007-11-20 Thread Jochem Maas
Peter Ford wrote:
> Jochem Maas wrote:
>> Kiketom wrote:
>>> Hi all.
>>> Yesterday i have looking for the overloading members
>>>
>>> Member overloading
>>> void __set ( string name, mixed value )
>>> mixed __get ( string name )
>>>
>>> As an example i put this code:
>>>
>>> class foo
>>> {
>>> private $ID;
>>> private $Name;
>>> private $LastName;
>> when you declare these three as 'real' members, __get() and __set()
>> will no longer be called - they are only called for non-existent members.
>>
> Nope, that's not true. 

Indeed. I did post back to say I was talking .

If the members are private, or otherwise inaccessible,

I believe that this was not always the case (i.e. that in
older versions private members behaved the same way as public
members with regard to __get()/__set() - I'm not sure but I think so)

> __get() and __set() are called - I've used this in a few places to provide a
> "read-only" member variable, e.g.:
> 
> class foo
> {
>   private $bar=0;
> 
>   public function __get($nm)
>   {
>   return $this->$nm;
>   }
> 
>   public function __set($nm,$val)
>   {
>   if ($nm != 'bar')
>   {
>   $this->$nm = $val;
>   }
>   }
> }
> 

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



Re: [PHP] overloading members. aghhh!!!

2007-11-20 Thread Peter Ford
Jochem Maas wrote:
> Kiketom wrote:
>> Hi all.
>> Yesterday i have looking for the overloading members
>>
>> Member overloading
>> void __set ( string name, mixed value )
>> mixed __get ( string name )
>>
>> As an example i put this code:
>>
>> class foo
>> {
>> private $ID;
>> private $Name;
>> private $LastName;
> 
> when you declare these three as 'real' members, __get() and __set()
> will no longer be called - they are only called for non-existent members.
> 
Nope, that's not true. If the members are private, or otherwise inaccessible,
__get() and __set() are called - I've used this in a few places to provide a
"read-only" member variable, e.g.:

class foo
{
private $bar=0;

public function __get($nm)
{
return $this->$nm;
}

public function __set($nm,$val)
{
if ($nm != 'bar')
{
$this->$nm = $val;
}
}
}

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Kiketom
Ok, no problem
;)

"Jochem Maas" <[EMAIL PROTECTED]> escribió en el mensaje 
news:[EMAIL PROTECTED]
> Kiketom wrote:
>> Then if i make this
>>
>> $foo = new foo();
>> $foo->ID = 12;
>>
>> what is happening??
>> if __Set and __Get only works if a member isn't exits,
>>
>> How come I do this?
>> $foo->ID = 12;
>> if the member $ID is private???
>>
>> I thought that this was possible because i have declared the two method 
>> set
>> and get :P
>>
>
> ignore my previous reply - it seems it was completely wrong. 

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Jochem Maas
Kiketom wrote:
> Then if i make this
> 
> $foo = new foo();
> $foo->ID = 12;
> 
> what is happening??
> if __Set and __Get only works if a member isn't exits,
> 
> How come I do this?
> $foo->ID = 12;
> if the member $ID is private???
> 
> I thought that this was possible because i have declared the two method set 
> and get :P
> 

ignore my previous reply - it seems it was completely wrong.

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Kiketom
Then if i make this

$foo = new foo();
$foo->ID = 12;

what is happening??
if __Set and __Get only works if a member isn't exits,

How come I do this?
$foo->ID = 12;
if the member $ID is private???

I thought that this was possible because i have declared the two method set 
and get :P


"Jochem Maas" <[EMAIL PROTECTED]> escribió en el mensaje 
news:[EMAIL PROTECTED]
> Kiketom wrote:
>> Hi all.
>> Yesterday i have looking for the overloading members
>>
>> Member overloading
>> void __set ( string name, mixed value )
>> mixed __get ( string name )
>>
>> As an example i put this code:
>>
>> class foo
>> {
>> private $ID;
>> private $Name;
>> private $LastName;
>
> when you declare these three as 'real' members, __get() and __set()
> will no longer be called - they are only called for non-existent members.
>
> so instead dump your data in an array or something
>
> private $data  = array(
> 'ID' => null,
> 'Name' => null,
> 'LastName' => null,
> );
>
>>
>> private function __get($var)
>> {
>> return $var;
>
> there is no such thing as 'implicit class scope' $var will not refer to
> $this->var as you seem to expect.
>
> return isset($this->data[$var]) ? $this->data[$var] : null;
>
>> }
>>
>> private function __set($var,$value)
>> {
>> $var = $value;
>
> same thing here.
>
>   if (array_key_exists($var, $this->data)
> $this->data[$var] = $value;
>
>> }
>> }
>> 

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Jochem Maas
Kiketom wrote:
> Hi all.
> Yesterday i have looking for the overloading members
> 
> Member overloading
> void __set ( string name, mixed value )
> mixed __get ( string name )
> 
> As an example i put this code:
> 
> class foo
> {
> private $ID;
> private $Name;
> private $LastName;

when you declare these three as 'real' members, __get() and __set()
will no longer be called - they are only called for non-existent members.

so instead dump your data in an array or something

private $data  = array(
'ID' => null,
'Name' => null,
'LastName' => null,
);

> 
> private function __get($var)
> {
> return $var;

there is no such thing as 'implicit class scope' $var will not refer to
$this->var as you seem to expect.

return isset($this->data[$var]) ? $this->data[$var] : null;

> }
> 
> private function __set($var,$value)
> {
> $var = $value;

same thing here.

  if (array_key_exists($var, $this->data)
$this->data[$var] = $value;

> }
> }
> 

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Kiketom
Ok, this is a great solution
Thanks ;)

"Robert Cummings" <[EMAIL PROTECTED]> escribió en el mensaje 
news:[EMAIL PROTECTED]
> On Mon, 2007-11-19 at 11:25 +0100, Kiketom wrote:
>> Hi all.
>> Yesterday i have looking for the overloading members
>>
>> Member overloading
>> void __set ( string name, mixed value )
>> mixed __get ( string name )
>>
>> As an example i put this code:
>>
>> class foo
>> {
>> private $ID;
>> private $Name;
>> private $LastName;
>>
>> private function __get($var)
>> {
>> return $var;
>> }
>>
>> private function __set($var,$value)
>> {
>> $var = $value;
>> }
>> }
>>
>>
>> $foo = new foo();
>> $foo->ID = 1;
>> $foo->Name = "Henry";
>> $foo->LastName = "Ford",
>> 
>>
>> that's horrible!!!
>>
>> And if i want to validate that ID > 0??
>>
>> i have to put this validation in the function __set for each property??
>> private function __set($var,$value)
>> {
>> if ($var = 'ID')
>> {
>> //validate that ID is > 0
>> }
>> $var = $value;
>> }
>>
>>
>> Not exists a better method to manage the properties in a class?
>>
>> Like in C#
>>
>> private int _ID;
>>
>> public int ID
>> {
>> get { return _ID;}
>> set
>> {
>> if (value > 0)
>> {
>> _ID = value;
>> }
>>  else
>>  {
>>  //Exception
>>  }
>> }
>> }
>
> Well, if you really want to, you can do the following:
>
> 
> class foo
> {
>private $ID;
>private $Name;
>private $LastName;
>
>private function __get( $var )
>{
>if( method_exists( $this, '___get_'.$var ) )
>{
>return $this->{'___get_'.$var}();
>}
>else
>{
>return $this->{$var};
>}
>}
>
>private function __set( $var, $value )
>{
>if( method_exists( $this, '___get_'.$var ) )
>{
>return $this->{'___set_'.$var}( $value );
>}
>else
>{
>return ($this->{$var} = $value);
>}
>}
>
>private function ___get_ID()
>{
>}
>
>private function ___set_ID( $value )
>{
>}
> }
>
> ?>
>
> But I wouldn't.
>
> Cheers,
> Rob.
> -- 
> ...
> SwarmBuy.com - http://www.swarmbuy.com
>
>Leveraging the buying power of the masses!
> ... 

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Robert Cummings
On Mon, 2007-11-19 at 06:27 -0500, Robert Cummings wrote:
> On Mon, 2007-11-19 at 11:25 +0100, Kiketom wrote:
> > Hi all.
> > Yesterday i have looking for the overloading members
> > 
> > Member overloading
> > void __set ( string name, mixed value )
> > mixed __get ( string name )
> > 
> > As an example i put this code:
> > 
> > class foo
> > {
> > private $ID;
> > private $Name;
> > private $LastName;
> > 
> > private function __get($var)
> > {
> > return $var;
> > }
> > 
> > private function __set($var,$value)
> > {
> > $var = $value;
> > }
> > }
> > 
> > 
> > $foo = new foo();
> > $foo->ID = 1;
> > $foo->Name = "Henry";
> > $foo->LastName = "Ford",
> > 
> > 
> > that's horrible!!!
> > 
> > And if i want to validate that ID > 0??
> > 
> > i have to put this validation in the function __set for each property??
> > private function __set($var,$value)
> > {
> > if ($var = 'ID')
> > {
> > //validate that ID is > 0
> > }
> > $var = $value;
> > }
> > 
> > 
> > Not exists a better method to manage the properties in a class?

Why don't you use a switch btw?



Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

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



Re: [PHP] overloading members. aghhh!!!

2007-11-19 Thread Robert Cummings
On Mon, 2007-11-19 at 11:25 +0100, Kiketom wrote:
> Hi all.
> Yesterday i have looking for the overloading members
> 
> Member overloading
> void __set ( string name, mixed value )
> mixed __get ( string name )
> 
> As an example i put this code:
> 
> class foo
> {
> private $ID;
> private $Name;
> private $LastName;
> 
> private function __get($var)
> {
> return $var;
> }
> 
> private function __set($var,$value)
> {
> $var = $value;
> }
> }
> 
> 
> $foo = new foo();
> $foo->ID = 1;
> $foo->Name = "Henry";
> $foo->LastName = "Ford",
> 
> 
> that's horrible!!!
> 
> And if i want to validate that ID > 0??
> 
> i have to put this validation in the function __set for each property??
> private function __set($var,$value)
> {
> if ($var = 'ID')
> {
> //validate that ID is > 0
> }
> $var = $value;
> }
> 
> 
> Not exists a better method to manage the properties in a class?
> 
> Like in C#
> 
> private int _ID;
> 
> public int ID
> {
> get { return _ID;}
> set
> {
> if (value > 0)
> {
> _ID = value;
> }
>  else
>  {
>  //Exception
>  }
> }
> }

Well, if you really want to, you can do the following:

{'___get_'.$var}();
}
else
{
return $this->{$var};
}
}

private function __set( $var, $value )
{
if( method_exists( $this, '___get_'.$var ) )
{
return $this->{'___set_'.$var}( $value );
}
else
{
return ($this->{$var} = $value);
}
}

private function ___get_ID()
{
}

private function ___set_ID( $value )
{
}
}

?>

But I wouldn't.

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

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