doukunsan5553
2017-01-26 16:46
浏览 88
已采纳

PHP call_user_func_array实例内部实例

<?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.

图片转代码服务由CSDN问答提供 功能建议

 &lt;?php 
 
class foo 
 {
 private $ callVar = false; \  n private $ constructorVar = false; 
 
 public function __construct()
 {
 $ this-&gt; constructorVar = get_class($ this); 
} 
 
 public function __call($ methodName,$ arguments)  )
 {
 $ this-&gt; callVar = get_class($ this); 
 call_user_func_array(array($ this,$ methodName),$ arguments); 
} 
 
受保护的函数方法($ params)  
 {
 echo('&lt; br&gt; Call var:'。$ this-&gt; callVar。'&lt; br&gt; Constructor var:'。$ this-&gt; constructorVar); 
} 
} 
  
class foo_sec extends foo 
 {
 protected function method($ params)
 {
 echo'First call:'; 
 parent :: method($ params); 
 echo'&lt; br&gt; 第二个调用:'; 
 $ test = new foo_trd(); 
 $ test-&gt;方法('param2'); 
} 
} 
 
class foo_trd extends foo 
 {
 
}  
 
 $ m = new foo_sec(); 
 $ m-&gt; method('param'); 
 
?&gt; 
   
 
 

给出了这个结果:

 第一次调用:
调用var:foo_sec 
Constructor var:foo_sec 
Second call:
Call var:
Constructor var:foo_trd 
    
 
 

此代码应返回当前正在运行的实例的名称,但如果实例位于另一个实例中,则类名在第二个实例中为空。 这是PHP错误还是其他一些我遗漏的问题?

换句话说,第二次调用中的 Call var 应该是 foo_trd < / code> - 不是空的。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题