我正在尝试解决一个问题:
function f($callable_function){
print_r($callable_function);// doesn't work at all, of course
}
f(function(){echo "hello World"});
Run Code Online (Sandbox Code Playgroud)
我Closure Object使用 print_r 函数获取该元素。有没有办法获得:
//OUTPUT RESULT
echo "hello World";
Run Code Online (Sandbox Code Playgroud)
编辑
这样做的目的是获取字符串中的函数声明(稍后用于数据库)。f 函数可以由任何开发人员使用,因此我们的想法是使其尽可能简单,这意味着我不希望开发人员在字符串内声明他的函数。
从未关闭过这个问题,但自 2014 年以来我就有了答案:
$reflFunc = new ReflectionFunction($callable);
$filename = $reflFunc->getFileName());
$startline = $reflFunc->getStartLine();
$endline = $reflFunc->getEndLine();
Run Code Online (Sandbox Code Playgroud)
使用preg_match,我可以提取我需要的内容。(如第一个答案所建议)
| 归档时间: |
|
| 查看次数: |
1217 次 |
| 最近记录: |