我有一个已经定义的闭包,我想在执行它时注入代码.这是一个例子:
$predefined = "print 'my predefined injected code<br />';";
$closure = function () {
print 'hello<br />';
};
call_user_func_array($closure, array());
// output : hello
Run Code Online (Sandbox Code Playgroud)
我想混合2个代码:一个预定义的代码和一个代码的代码.修改后,我希望我的闭包看起来像这样
$closure = function () {
print 'my predefined injected code<br />';
print 'hello<br />';
};
Run Code Online (Sandbox Code Playgroud)
在执行之前是否可以在闭包中插入一些代码?
注意:我不能使用将代码作为字符串的"create_function",因此可以轻松修改.闭包已经定义并以某种方式定义(通过一个带回调arg而不是字符串arg的函数).
谢谢你的帮助.
编辑:
这是解决方案
function hackClosure($closure, $inject_code)
{
$reflection = new ReflectionFunction($closure);
$tmp = $reflection->getParameters();
$args = array();
foreach ($tmp as $a) array_push($args, '$'.$a->getName() . ($a->isDefaultValueAvailable() ? '=\''.$a->getDefaultValue().'\'' : ''));
$file = new SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine()-1);
$code = '';
while …
Run Code Online (Sandbox Code Playgroud)