Re: magic getter
| From: | Jim Lucas | Date: | Thu, 19 Jul 2012 19:39:08 +0000 |
| Subject: | Re: magic getter | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318522@lists.php.net to get a copy of this message | ||
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/