<?php
class foo
{
private $ callVar = false; \ n 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);
}
受保护的函数方法($ 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> 第二个调用:';
$ test = new foo_trd();
$ test->方法('param2');
}
}
class foo_trd extends foo
{
}
$ m = new foo_sec();
$ m-> method('param');
?>
code> pre>
给出了这个结果: p>
第一次调用:
调用var:foo_sec
Constructor var:foo_sec
Second call:
Call var:
Constructor var:foo_trd
code> pre>
此代码应返回当前正在运行的实例的名称,但如果实例位于另一个实例中,则类名在第二个实例中为空。 这是PHP错误还是其他一些我遗漏的问题? p>
换句话说,第二次调用中的 Call var code>应该是 foo_trd < / code> - 不是空的。 p>
div>
<?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');
?>
Gives this result:
First call:
Call var: foo_sec
Constructor var: foo_sec
Second call:
Call var:
Constructor var: foo_trd
This code should return the name of the instance that is currently running, but if the instance is inside another instance, the class name is empty in second one. Is this a PHP bug or some other issue I'm missing?
In other words, the Call var
in the second call should be foo_trd
- not empty.