Magic __get in php
810
i need help about this below:
i understand the code but i do not understand the output of the last line
why the output isb,A,B
and notA,b,B
?
class magic{
public $a = "A";
protected $b = array("a" => "A", "b" => "B","c" => "C");
protected $c = array(1,2,3);
public function __get($name){
echo "$name,";
return $this -> b[$name];
}
}
$m = new magic();
echo $m->a;
// A
// because $a is public
echo $m->b;
// b,B
// because $b is protected
echo $m->a.",".$m->b;
// b,A,B
Answer
Solution:
The problem is that your echoing the name in your
__get()
method, this will output the value straight away, but return the value of the variable to display later.If you change the routine to...
Your output becomes -
A,b,B