does not show the expected result when use __get() in php -


<?php class classname { public $attribute; function __get($name) { return 'here:'.$this->$name; } function __set ($name, $value) { $this->$name = $value; } } $a = new classname(); $a->attribute = 5; echo $a->attribute; 

when run above script, shows:5

question:

echo $a->attribute; line of code invoke function __get($name), right? why not show: here:5 ?

the magic __get , __set , __call called if property properties or methods undefined or unaccessible calling scope, or undefined.

to make work have remove public reference attribute or make protected or private.

class classname {   protected $attribute;   function __get($name)   {     return 'here:'.$this->$name;   }   function __set ($name, $value)   {     $this->$name = $value;   } } $a = new classname(); $a->attribute = 5; //  calling __set echo $a->attribute; // calling __get 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -