Home  >  Article  >  Backend Development  >  在A文件中使用$this->display()调用另外一个类文件的方法,应该如何做

在A文件中使用$this->display()调用另外一个类文件的方法,应该如何做

WBOY
WBOYOriginal
2016-06-13 13:48:481154browse

在A文件中使用$this->display()调用另外一个类文件的方法,应该怎么做?
A文件:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->class indexAction
{
    public function index()
    {
        echo 'abc';
        $this->display(); //就是这一行
    }

}


B类文件:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->class view {

    public function display() {
        echo '模板输出成功';
    }
}


要怎么实例化才能在A文件中可以这样$this->display()调用另外B类文件

------解决方案--------------------
$this->display() ? 这种设计很奇怪
恐怕得靠继承吧 class indexAction extends view
或者可以选择魔术方法 __call()
现学现卖:
PHP code
class indexAction
{
    //新增一个魔术方法
    public function __call($mName, $mArg)
    {
        $view = new view();
        if(method_exists($view, $mName))
            call_user_func(array($view, $mName));
    }
} <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn