加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:39:34 所属栏目:PHP教程 来源:网络整理
导读:有没有获取类或方法的源代码的方法?我正在看 ReflectionClass,但我没看到. 解决方法 我能想到的最好: $class = new ReflectionClass($c);$fileName = $class-getFileName();$startLine = $class-getStartLine()-1; // getStartLine() seems to start after
有没有获取类或方法的源代码的方法?我正在看 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 …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读