我遇到了以下代码,无法弄清楚为什么使用 php 的 get 和 set 魔术方法以非直观的顺序输出脚本的输出。
class Magic
{
public $a = "A";
protected $b = array("a" => "A", "b" => "B", "c" => "C");
protected $c = array(1,2,3);
public function __get($v)
{
echo "$v";
return $this->b[$v];
}
public function __set($var, $val)
{
echo "$var: $val";
$this->$var = $val;
}
}
$m = new Magic();
echo $m->a . "," . $m->b . "," . $m->c . ",";
$m->c = "CC";
echo $m->a . "," . $m->b . "," . $m->c;
输出是:
bcA,B,C,c
CCbcA,B,C
首先,为什么它不输出 A 作为第一件事?输出顺序没有意义。