有没有获取类或方法的源代码的方法?我正在看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 …
标签:php,reflection,introspection
来源: https://codeday.me/bug/20190613/1235438.html