Re: magic getter

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 07/19/2012 12:22 PM, Sebastian wrote:
Hi all,

is this a bug, or a feature?

class Foo
{
private $data;

public function __get($name)
{
return $this->data[$name];
}
}

$foo = new Foo();
$foo->color = 'red';

echo $foo->color;

I would expect an error, or a least a notice, but it prints out "red" ...



Their is nothing magical about this.

When you do this:

$foo = new Foo();
$foo->color = 'red';

you create a variable within the $foo instance variable called color with a value of red.

Then, later on when you do this

echo $foo->color;

you are then simply echo'ing the variable/value you created.

with this example, you are never using the __get() magic function to retrieve the value of color.


--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux