我需要这方面的帮助:
我理解代码,但我不理解最后一行的输出
为什么输出为b,A,B
而不是A,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
答案 0 :(得分:3)
问题是您在__get()
方法中回显名称,这将立即输出值,但返回变量的值以便稍后显示。
如果您将例程更改为...
public function __get($name){
//echo "$name,";
return "$name,".$this -> b[$name];
}
您的输出变为 - A,b,B