Re: overloading members. aghhh!!!
| From: | Kiketom | Date: | Mon, 19 Nov 2007 11:59:42 +0000 |
| Subject: | Re: overloading members. aghhh!!! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-264773@lists.php.net to get a copy of this message | ||
Ok, this is a great solution
Thanks ;)
"Robert Cummings" <robert@interjinn.com> escribió en el mensaje
news:1195471647.1702.40.camel@blobule...
> 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:
>
> <?php
>
> 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!
> ...........................................................