考虑这个类:
class test
{
public function __set($n, $v)
{
echo "__set() called\n";
$this->other_set($n, $v, true);
}
public function other_set($name, $value)
{
echo "other_set() called\n";
$this->$name = $value;
}
public function t()
{
$this->t = true;
}
}
我正在重载 PHP 的魔法__set()
方法。每当我在test
类的对象中设置属性时,它都会调用__set()
,而后者又会调用other_set()
.
$obj = new test;
$test->prop = 10;
/* prints the following */
__set() called
other_set() called
但other_set()
有以下行$this->$name = $value
。这不应该导致调用__set()
,导致无限递归吗?
我推测它__set()
只有在课外设置时才会调用。但是,如果您调用该方法t()
,您也可以清楚地看到它也通过__set()
了。