2011-01-31 50 views
0

我的所有网站共享一个共同的首发,处理网址,文件位置等。有3种情况需要处理 - 是目录,文件存在和文件不存在。每个应用程序对每种情况都有唯一的代码我决定修改一下runkit,并试图统一代码。每个案例将由一个函数处理,可以通过runkit重新定义。PHP - 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 
+0

你是什么意思“这不工作”?编译错误?或者是什么?你如何发现不工作的东西? – 2011-01-31 09:50:59

+0

我更新了我的问题 – realshadow 2011-01-31 10:38:04

回答

0

{...去除不必要的.. }

====编辑=====

[对于新的问题,你需要的是这个]

<? 
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(); 

?> 
0

call_user_func函数的文档说第一个参数是'callable'。因此要动态调用类方法,您应该通过array($obj, 'func_name')