PHP:触发一个Method,忽略返回内容

PHP: Trigger a Method and ignore the returning content

我在 Whosebug 上的第一个问题!

首先:感谢您的帮助,抱歉我的英语不好。 ^^

我尝试获取值,child class 方法给出了他的 parent class 方法。问题是, parent class 方法用 exit; 终止当前脚本 + 我无权访问 child class(es) + 我不想编辑 'one class'(见下文)。

代码,为了更好的理解:

<?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 :: 使用反射Class

我试图读取索引方法的内容,在"two class"里面。

所以我抓到了index函数的源码,把里面的参数分开了 $this->display 方法调用。

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

尝试 2 :: 使用 ob_start()

我试图从视图 Class 中加载 returns 的内容到缓冲区中。但是 exit; 命令 完全破坏 php 代码并直接打印输出(我不想要输出 'View' Class).

代码:

class myclass extends one{
    public function myfunc(){
        ob_start();
            $something = new two();
            $something->index();
        ob_end_clean();

        $param1 = $this->classVAR;
        $param2 = $this->classVAR2;
    }
}

想法

是否可以临时调用("two class" 的)索引方法,而不会完全中断 通过 exit; 行?

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

或者两个class中的parent是否可以"change"?

你有没有其他想法,我该如何解决这个问题?但请不要实验 PHP 代码, 或其他 PHP 扩展/库。

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

此致 山姆

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

class one{
    //...

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

第二个选项怎么样?

自定义 class 扩展 两个 class

class myclass extends two{

    public function display($param1, $param2, $exit = false){
        parent::display($param1, $param2, $exit);
    }
}

$something = new myclass();
$something->index();