获取用户定义的类和函数的源代码? [英] Get source code of user-defined class and functions?

查看:85
本文介绍了获取用户定义的类和函数的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

最好的是我想出的:

$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);
}

不适用于这样定义的类:

This doesn't work well with classes defined like this:

class Foo { }

说这是2行,当它只能是1 ...

Says this is 2 lines long when it should only be 1...

这篇关于获取用户定义的类和函数的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆