获取用户定义的类和函数的源代码?

mpe*_*pen 5 php reflection introspection

有没有获取类或方法的源代码的方法?我在看ReflectionClass,但我没有看到.

mpe*_*pen 8

我能想到的最好:

$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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:这不适用于这样定义的类:

class Foo { }
Run Code Online (Sandbox Code Playgroud)

说这是2行长,它应该只有1 ...