我所有的网站都有一个共同的起始点,它处理urls、文件位置等等。有3种情况需要处理-是目录,文件存在和文件不存在。每个应用程序对每种情况都有唯一的代码。我决定稍微修改一下runkit,并且我正在尝试统一代码。每个案例都将由一个函数处理,该函数可以通过runkit重新定义。
请考虑以下代码:
class start {
function __construct() {
$this->options = array();
}
public function process() {
// some code here
$this->file_not_exists();
}
public function file_not_exists() {
$this->options['page'] = 222;
}
public function redefine($what, $code) {
runkit_method_redefine(get_class($this), $what, '', $code, RUNKIT_ACC_PUBLIC);
}
}
$start = new start();
$start->redefine('file_not_exists', '$this->options["page"] = 333;')
// page is now 333本部分按预期工作。但是,当我试图更改代码时,重新定义的方法调用用户函数时,它就能工作了。但是,为了上帝的爱,我想不出如何把$this传递给这个函数。
重新定义方法如下所示:
public function redefine($what, $code) {
runkit_method_redefine(get_class($this), $what, '', 'call_user_func('.$code.'(), '.$this.');', RUNKIT_ACC_PUBLIC)
}无论我尝试什么,这都不起作用(call_user_func_array也是如此)。我就是搞不懂。请记录在案:
public function redefine($what, $code) {
my_user_function($this);
}确实有用。
任何帮助都是非常感谢的。
请注意,这只是一个实验,我想知道如何做这个:)
编辑:我得到:
Catchable fatal error: Object of class starter could not be converted to string in blablallala\rewrite_starter2.php on line 153发布于 2011-01-31 10:13:50
{..。无必要移除.}
====编辑=====
对于新的问题,你需要的是
<?
class start {
function __construct() {
$this->options = array();
}
public function process() {
// some code here
$this->file_not_exists();
}
public function file_not_exists() {
$this->options['page'] = 222;
}
public function redefine($what, $code) {
runkit_method_redefine(get_class($this),
$what,
'',
'call_user_func(array($this,' .$code. '),$this);',
RUNKIT_ACC_PUBLIC);
}
public function my_func($someobj)
{
print_r($someobj);
}
}
$start = new start();
$start->redefine('file_not_exists', 'my_func');
$start->process();
?>发布于 2012-09-09 20:13:25
关于call_user_func函数的文档说,第一个参数是'可赎回‘。因此,要动态调用类方法,应该传递array($obj, 'func_name')。
https://stackoverflow.com/questions/4849290
复制相似问题