Is there a method for getting the source code of a class or method? I'm looking at the ReflectionClass, but I don't see one.
Best I could come up with:
$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: This doesn't work well with classes defined like this:
class Foo { }
Says this is 2 lines long when it should only be 1...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With