是否有获得类或方法的源代码的方法?我正在看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);
}
Edit:对于这样定义的类,这不能很好地工作:
class Foo { }
表示这是2行,而它应该只有1行…