是否有获取类或方法源代码的方法?我在看ReflectionClass,但我没看到。
答案 0 :(得分:7)
我能想到的最好:
$class = new ReflectionClass($c);
$fileName = $class->getFileName();
$startLine = $class->getStartLine()-1; // getStartLine() seems to start after the {, we want to include the signature
$endLine = $class->getEndLine();
$numLines = $endLine - $startLine;
if(!empty($fileName)) {
$fileContents = file_get_contents($fileName);
$classSource = trim(implode('',array_slice(file($fileName),$startLine,$numLines))); // not perfect; if the class starts or ends on the same line as something else, this will be incorrect
$hash = crc32($classSource);
}
编辑:这对于定义如下的类不适用:
class Foo { }
当它应该只有1 ...
时,这是2行长