PHP - runkit 重新定义方法
我的所有网站都共享一个通用的启动器,用于处理 url、文件位置等。有 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
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. Each application has unique code for each case. I decided to tinker with runkit a bit and I am trying to unify the code. Each case will be handled by a function, that could be redefined via 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.
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). 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
{...删除不必要的...}
====编辑=====
[对于新问题,你需要的是这个]
{... removed as not necessary ...}
==== EDIT =====
[For the new problem, what you need is this]
关于
call_user_func
函数的文档说第一个参数是 '可调用'。因此,要动态调用类方法,您应该传递array($obj, 'func_name')
。The documentation on the
call_user_func
function says that the first argument is 'callable'. So to call the class method dynamically, you should pass thearray($obj, 'func_name')
.