简体   繁体   English

PHP-Runkit重新定义方法

[英]PHP - runkit redefine method

all my websites share a common starter, that deals with urls, file locations, etc.. There are 3 cases that need to be handled - is directory, file exists and file does not exist. 我所有的网站都共享一个通用的启动程序,该启动程序处理url,文件位置等。有3种情况需要处理-目录,文件存在和文件不存在。 Each application has unique code for each case. 每个应用程序在每种情况下都有唯一的代码。 I decided to tinker with runkit a bit and I am trying to unify the code. 我决定稍微修改一下runkit,然后尝试统一代码。 Each case will be handled by a function, that could be redefined via runkit. 每种情况将由一个函数处理,可以通过runkit重新定义。

Consider this code: 考虑以下代码:

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 part works as intended. 这部分按预期工作。 But when I try to change the code so the redefined method calls user function it works. 但是,当我尝试更改代码以使重新定义的方法调用用户函数时,它可以工作。 But, for the love of god, I cant figure out how to pass the $this to the function. 但是,出于对上帝的爱,我不知道如何将$ this传递给函数。

Redefine method looks like this: 重新定义方法如下所示:

public function redefine($what, $code) {
    runkit_method_redefine(get_class($this), $what, '', 'call_user_func('.$code.'(), '.$this.');', RUNKIT_ACC_PUBLIC)
}

This doesn't work, no matter what I try (call_user_func_array as well). 无论我如何尝试,这都行不通(以及call_user_func_array)。 I just cant figure it out. 我只是想不通。 For the record: 作为记录:

public function redefine($what, $code) {
    my_user_function($this);
}

Does work. 确实有效。

Any help is appreciated. 任何帮助表示赞赏。

Note that this is just an experiment and I would like to know how to do this :) 请注意,这只是一个实验,我想知道如何做:)

Edit: I get: 编辑:我得到:

Catchable fatal error: Object of class starter could not be converted to string in blablallala\rewrite_starter2.php on line 153

The documentation on the call_user_func function says that the first argument is ' callable '. call_user_func函数的文档中说,第一个参数是' callable '。 So to call the class method dynamically, you should pass the array($obj, 'func_name') . 因此,要动态调用类方法,应传递array($obj, 'func_name')

{... removed as not necessary ...} {...不必要移除...}

==== EDIT ===== ====编辑=====

[For the new problem, what you need is this] [对于新问题,您需要的是这个]

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM