重构/获取 PHP 函数的源代码 [英] Reconstruct / get source code of a PHP function
本文介绍了重构/获取 PHP 函数的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过函数名称以编程方式获取函数的源代码吗?
Can I programmatically get the source code of a function by its name?
喜欢:
function blah($a, $b) { return $a*$b; }
echo getFunctionCode("blah");
有可能吗?
有没有php自描述函数来重构函数/类代码?(我的意思是不要从源文件中获取源代码.)
Are there any php self-descriptive functions to reconstruct function/class code? (I mean instead of getting source code right from the source file.)
在 Java 中存在:http://java.sun.com/developer/technicalArticles/ALT/Reflection/
In Java there exists: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
推荐答案
扩展使用 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屋!
查看全文