PHP:触发一个方法并忽略返回的内容


PHP: Trigger a Method and ignore the returning content

我关于Stackoverflow的第一个问题!

首先:谢谢你的帮助,对不起我的英语不好。 ^^

我尝试获取子类方法为其父类方法提供的值。问题是,父类方法终止当前脚本,exit; + 我无权访问子类 + 我不想编辑"一个类"(见下文)。

代码,为了更好地理解:

<?php
    // The Controller
    class one{
        // [...]
        public $classVAR = "";
        public $classVAR2 = "";
        public function render($param1, $param2){
            // This are the variables that I need.
            $this->classVAR = $param1;
            $this->classVAR2 = $param2;
            return new View(); // Returns the Page Content
        }
        public function display($param1, $param2, $exit = true){
            echo $this->render($param1, $param2);
            if($exit === true){
                exit;
            }
        }
        // [...]
    }
    // Another Class to which I have no control (ex. "Plugin Classes")
    class two extends one{
        // [...]
        public function index(){
            // Some other Stuff
            //  string  $stuff
            //  array   $otherstuff
            $this->display($stuff, $otherstuff);
        }
        // [...]
    }
?>

尝试 1 :: 使用反射类

我试图在"两个类"内阅读索引方法的内容。

所以我捕获了索引函数的源代码,并将 $this->display方法调用。

问题:在大多数情况下,$otherstuff变量包含一个(或多个)数组"key=>option"对,可由视图类使用。还有我为此使用的"两类"实验,包含对'something' => $this->loadOptions(),并且此方法受到保护。

尝试 2 :: 使用 ob_start()

我尝试在缓冲区中加载从视图类返回的内容。但是exit;命令完全破坏 PHP 代码并直接打印输出(我不希望输出来自"查看"类)。

代码:

class myclass extends one{
    public function myfunc(){
        ob_start();
            $something = new two();
            $something->index();
        ob_end_clean();
        $param1 = $this->classVAR;
        $param2 = $this->classVAR2;
    }
}

思潮

是否可以临时调用索引方法("两个类")而不完全中断通过exit;线?

或者是否可以在缓冲区中加载"two"类,然后操作$this->display 方法?如果是,那么我可以添加第三个参数,这将禁用exit;行。

还是可以"改变"两个类的父级?

您还有其他想法吗,我该如何解决这个问题?但是请不要使用实验性的PHP代码,或其他 PHP 扩展/库。

谢谢!(我希望我能很好地解释我的问题。

真诚的你,山 姆。

您是否尝试将默认值$exit设置为 false?

class one{
    //...
     public function display($param1, $param2, $exit = false){
        // ...
    }
}

怎么样 - 第二种选择 ?

自定义类扩展了这两个

class myclass extends two{
    public function display($param1, $param2, $exit = false){
        parent::display($param1, $param2, $exit);
    }
}
$something = new myclass();
$something->index();