我正在PHP中編寫api。 我有一個基類implemets神奇功能__call
:PHP錯誤:基類函數調用派生類
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]
我無法找到的錯誤,但如果功能測試是保護一切工作正常。
溶液中發現:
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);
}
大家知道,你的英語比我認識的母語人士要好。 – nickb
你應該只能調用在類之外公開的函數,除非你使用反射:http://php.net/reflectionmethod.setaccessible –
謝謝你nickb :) – Naibaf