<?php
class foo
{
private $callVar = false;
private $constructorVar = false;
public function __construct()
{
$this->constructorVar = get_class($this);
}
public function __call($methodName, $arguments)
{
$this->callVar = get_class($this);
call_user_func_array(array($this, $methodName), $arguments);
}
protected function method($params)
{
echo('<br>Call var: '.$this->callVar.'<br>Constructor var: '.$this->constructorVar);
}
}
class foo_sec extends foo
{
protected function method($params)
{
echo 'First call: ';
parent::method($params);
echo '<br> Second call: ';
$test = new foo_trd();
$test->method('param2');
}
}
class foo_trd extends foo
{
}
$m = new foo_sec();
$m->method('param');
?>
內實例給出了這樣的結果:PHP call_user_func_array例如
First call:
Call var: foo_sec
Constructor var: foo_sec
Second call:
Call var:
Constructor var: foo_trd
此代碼應返回當前正在運行的實例的名稱,但如果實例是另一個實例中,類名是空的在第二個。這是一個PHP錯誤或我缺少的其他問題?
換句話說,第二次調用中的Call var
應該是foo_trd
- 不爲空。
你的問題並不在於call_user_func_array(),而是使用php調用magic函數__call。 – Rahi
@Rahi但是爲什麼?這是一個全新的例子 – dfilkovi
@RyanVincent我已經使用另一種方法解決了它,但我徘徊爲什麼這不是正確的工作原因它應該 – dfilkovi