我有一个名为$obj的对象。我已经覆盖了该类的 __call
函数,因此当我调用 $obj->setVariableName($value)
时,会发生这种情况:$obj->variableName = $value
.我不知道项目中何时以及如何调用$obj->setVariableName($value)
。因此,在运行应用程序期间,会发生这种情况:
setVariable1($value) : works!
setVariable2($value) : works!
setVariable3($value) : It won't trigger __call()
setVariable4($value) : works!
当我编写额外的函数setVariable3
时,它就可以工作了。我不知道setVariable3
是如何调用的,是$obj->setVariable3
直接调用还是用call_user_func_array
这样的函数调用。
__call
不适合setVariable3
可能有什么问题?
更新:现在我知道setVariable3
是从$form->bind($user)
调用的,并且运行$user->setVariable3('foo')
工作。(这是一个ZF2+学说项目)
听起来很奇怪,但对我有用。
class test {
public function __call($method, $args)
{
printf("Called %s->%s(%s)n", __CLASS__, __FUNCTION__, json_encode($args));
}
}
$test = new test();
$value = 'test';
$test->setVariable1($value);
$test->setVariable2($value);
$test->setVariable3($value);
$test->setVariable4($value);
将输出:
Called test->__call(["test"])
Called test->__call(["test"])
Called test->__call(["test"])
Called test->__call(["test"])
并且仅当您尝试访问无法访问的属性时,才会调用__set例如
class test {
public function __set($var, $value) {
printf("Called %s->%s(%s)n", __CLASS__, __FUNCTION__, json_encode(func_get_args()));
}
}
$test = new test();
$value = 'test';
$test->callMagicSet = $value;
将导致:
Called test->__set(["callMagicSet","test"])