是否有获取类或方法源代码的方法?我正在看ReflectionClass,但没有看到。
最佳答案
最好的我想出了:
$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 { }
说这是2行长,当它只能是1时...