【问题标题】:How to extract Doc Comments from PHP scripts which are not using classes?如何从不使用类的 PHP 脚本中提取文档注释?
【发布时间】:2017-03-24 14:25:10
【问题描述】:

我知道 PHP ReflectionClass 可以从 PHP 脚本中获取 doc cmets。现在我遇到了数百个不使用类而只使用简单公共函数的旧 PHP 脚本。如果我是对的,ReflectionClass 不能在这里使用吗?我的目标是提取每个文档 cmets。例如。 test1.php 包含以下几行:

<?php
/**
 * some main comments here
 */
...some php lines here...

/**
 * function comment
 * Blah blah
 */
function foo()
{
 ...some php lines here...
}
...and more functions below each with comments

?>

所以我想要的输出是这样的表格:

--------------------------------------------------
| Filename  | FUNCTION | Comment                 |
--------------------------------------------------
| test1.php |          | some main comments here |
| test1.php | foo()    | function comment        |
|           |          | Blah Blah               |

【问题讨论】:

  • 您尝试过一些文档生成器吗? phpdox.dephpdoc.org
  • 穆埃沙,谢谢。还没有。如果您能进一步提示我,我将不胜感激:)

标签: php


【解决方案1】:

您可以使用ReflectionFunction

$functions = get_defined_functions();

foreach ($functions['user'] as $function) {

    $reflection = new ReflectionFunction($function);

    var_dump($reflection->getName());
    var_dump($reflection->getDocComment());
}

结果将是(如果您只定义了 foo 函数):

string(3) "foo"
string(43) "/**
 * function comment
 * Blah blah
 */"

【讨论】:

  • Matei,如何或在哪里指定文件test1.php?就我而言,我会让数百人说test1.phptest100.php。 ReflectionFunction 如何知道它当前正在处理哪个 PHP 文件?对不起,我可以用谷歌搜索的文档不多。
  • 马太,还有,test1.php开头的评论“这里有一些主要的cmets”呢?每次 PHP 文件的开头都有类似的 cmets。
  • 我必须将您的答案标记为正确,并且还必须从stackoverflow.com/questions/2197851/function-list-of-php-file 向 Joachim 致谢。
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2017-03-06
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多