我有以下代码(取自Zend认证练习):
<?php
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;
这输出以下内容:
b,c,A,B,C,c: CC,b,c,A,B,C
我理解的大部分输出,但第一个回声声明我不知道 - 输出可能是从&#34; A&#34;?开始的。毕竟,a是一个公共属性,它被正确定义,因此__get不会被调用,即使它被调用,我仍然不知道它将如何输出&#34; b&#34;。< / p>
如果我将第一个echo语句更改为:
echo $m->a;
输出:
A
正如所料。因此值在(完整)echo语句中发生变化,可能与优先级有关。
真的对这个输出的完整解释感兴趣。谢谢!
答案 0 :(得分:2)
你的专栏:
echo $m->a ."," .$m->b ."," .$m->c .",";
也可以这样写:
$text = $m->a ."," .$m->b ."," .$m->c .",";
echo $text;
如上所述,命令行为更清晰:
首先连接字符串,然后打印连接字符串:
$m->a => 'A,' is added to string $m->b => __get() print 'b,' then 'B,' is added to string $m->c => __get() print 'c,' then 'C,' is added to string echo => print 'A,B,C' (concatenated string)