重建/获取php函数代码 [英] reconstruct/get code of php function
本文介绍了重建/获取php函数代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过编程方式获得功能名称的代码吗?
Can I programmatically get a code of function by it's name?
赞:
function blah($a, $b) { return $a*$b; }
echo getFunctionCode("blah");
有可能吗?
是否有任何PHP自描述函数可重构函数/类代码?
(意味着不要从源文件中正确获取源代码)
Are there any php self-descriptive functions to reconstruct function/class code?
(mean instead of getting source code right from sources file)
在Java中存在: http://java.sun.com/developer/technicalArticles/ALT/Reflection /
在PHP中:
get_class_methods()
typeof
ReflectionFunction(感谢Alin Purcaru)
In PHP:
get_class_methods()
typeof
ReflectionFunction (thanks Alin Purcaru)
推荐答案
在扩大使用ReflectionFunction的建议的范围内,您可以使用类似以下的内容:
Expanding on the suggestion to use the ReflectionFunction, you could use something like this:
$func = new ReflectionFunction('myfunction');
$filename = $func->getFileName();
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($filename);
$body = implode("", array_slice($source, $start_line, $length));
print_r($body);
这篇关于重建/获取php函数代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文