我正在用 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 没有输出,只有网络浏览器错误。
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]
我找不到错误,但如果功能 Test 受到保护,一切正常。
找到的解决方案:
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);
}