我有一个问题,一直在破坏我想要做的事情很长一段时间.它与在PHP中使用魔法获取和设置以及尝试对对象进行预增量有关.我有一个类如下的PHP类:
class Foo {
public $object;
function __construct() {
$this->object = array("bar" => 1);
}
function & __get($name) {
return $this->object[$name];
}
function __set($name, $value) {
echo "Old value: ". $this->object[$name] ." - New value: ". $value ."\n";
$this->object[$name] = $value;
}
}
Run Code Online (Sandbox Code Playgroud)
注意&的__get方法.现在我运行这段代码:
$o = new Foo();
echo "First test\n";
$o->bar = 2;
echo "Second test\n";
$o->bar = $o->bar + 1;
echo "Third test\n";
$o->bar += 1;
echo "Fourth test\n";
++$o->bar;
Run Code Online (Sandbox Code Playgroud)
输出是:
First test
Old …Run Code Online (Sandbox Code Playgroud)