【发布时间】:2015-03-11 09:46:39
【问题描述】:
我在 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 :: 使用 ReflectionClass
我试图读取索引方法的内容,在“二类”里面。
所以我抓住了索引函数的来源,并在里面分离了参数
$this->display 方法调用。
问题:$otherstuff 变量包含 - 在大多数情况下 - 包含一个(或多个)的数组
“key=>option”对,可以被 View Class 使用。还有我用来做这个的“二等舱”
实验,包含'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; 行?
或者是否可以在缓冲区中加载“二”类,然后操作$this->display
方法?如果是,那么我可以添加第三个参数,这将禁用 exit; 行。
或者是否可以“改变”两个类的父类?
您还有其他想法吗,我该如何解决这个问题?但请不要实验性的 PHP 代码, 或其他 PHP 扩展/库。
谢谢! (我希望我能很好地解释我的问题。)
此致, 山姆。
【问题讨论】:
标签: php class oop exit ob-start