我正在用PHP编写一个api。我有一个基类,它实现了魔术函数__call
:
class Controller
{
public function __call($name, $arguments)
{
if(!method_exists($this,$name))
return false;
else if(!$arguments)
return call_user_func(array($this,$name));
else
return call_user_func_array(array($this,$name),$array);
}
}
还有这样的子班:
class Child extends Controller
{
private function Test()
{
echo 'test called';
}
}
所以当我这样做时:
$child = new Child();
$child->Test();
并加载页面 这需要很多时间,一段时间后,Web 浏览器打印出无法请求该页面。 没有给出来自PHP的输出,只有一个Web浏览器错误。
Apache 错误日志(仅限最后一部分):
...
[Tue Sep 24 12:33:14.276867 2013] [mpm_winnt:notice] [pid 1600:tid 452] AH00418: Parent: Created child process 3928
[Tue Sep 24 12:33:15.198920 2013] [ssl:warn] [pid 3928:tid 464] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]
[Tue Sep 24 12:33:15.287925 2013] [mpm_winnt:notice] [pid 3928:tid 464] AH00354: Child: Starting 150 worker threads.
[Tue Sep 24 12:38:43.366426 2013] [mpm_winnt:notice] [pid 1600:tid 452] AH00428: Parent: child process exited with status 3221225725 -- Restarting.
[Tue Sep 24 12:38:43.522426 2013] [ssl:warn] [pid 1600:tid 452] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]
我找不到错误,但如果功能测试受到保护,一切正常。
找到的解决方案:
public function __call($name, $arguments)
{
if(!method_exists($this,$name))
return false;
$meth = new ReflectionMethod($this,$name);
$meth->setAccessible(true);
if(!$arguments)
return $meth->invoke($this);
else
return $meth->invokeArgs($this,$arguments);
}
此行为是 method_exists()
文档中记录的问题(错误?):即使该方法是私有/受保护的,因此无法从类外部访问,method_exists()
也返回 true。这会导致您的情况下无限递归,因为您的Child->Test()
调用调用 Child::__call()
,它会检查Test()
是否存在(它确实存在,但无法调用),然后尝试调用它,这再次被调用__call()
。评论建议使用get_class_methods()
可能会解决问题。我不确定为什么将Test()
的可见性更改为private
会改变您所说的行为。
Test()
公开可见度,它应该可以工作。
我不完全确定为什么私有可见性会导致 500 错误(而不是 Call to private method...
),但我怀疑这与涉及__call()
函数的递归有关。PHP 中的某些功能弊大于利 - 你真的需要它吗?