首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP反射;提取非块注释

PHP反射;提取非块注释
EN

Stack Overflow用户
提问于 2011-06-11 06:45:08
回答 5查看 1.7K关注 0票数 1

我最近已经熟悉了Reflection,并且一直在试验它,特别是getDocComment(),但是它似乎只支持/** */评论块。

代码语言:javascript
复制
/** foobar */
class MyClass{}

$refl = new ReflectionClass('MyClass');

// produces /** foobar */
echo $refl->getDocComment();

-对抗.

代码语言:javascript
复制
# foobar
class MyClass{}

$refl = new ReflectionClass('MyClass');

// produces nothing
echo $refl->getDocComment();

难道不可能在不诉诸任何形式的file_get_contents(__FILE__)胡说八道的情况下捕捉到这一点吗?

根据dader51的回答,我认为我最好的方法是遵循以下思路:

代码语言:javascript
复制
// random comment

#[annotation]

/**
 * another comment with a # hash
 */

#[another annotation]

$annotations
    = array_filter(token_get_all(file_get_contents(__FILE__)), function(&$token){
    return (($token[0] == T_COMMENT) && ($token = strstr($token[1], '#')));
});

print_r($annotations);

产出:

代码语言:javascript
复制
Array
(
    [4] => #[annotation]

    [8] => #[another annotation]

)
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-06-11 06:52:43

与常规注释相比,DocComments通过说明如何使用类来区别自己,这些注释可以帮助开发人员阅读代码。这也是为什么方法不被称为getComment()的原因。

当然,这都是文本解析,而且有人在docComments中做了一个选择--总是这些多行注释,但这个选择显然已经做出了,而阅读常规注释并不属于反射类别。

票数 5
EN

Stack Overflow用户

发布于 2011-06-11 07:09:01

几天前我正试着做一个你,这就是我的诀窍。您只需使用php内部令牌程序( http://www.php.net/manual/en/function.token-get-all.php ),然后遍历返回的数组,只选择注释,下面是一个示例代码:

代码语言:javascript
复制
$a = token_get_all(file_get_contents('path/to/your/file.php'));
print_r($a); // display an array of all tokens found in the file file.php

以下是所有令牌php识别的列表:http://www.php.net/manual/en/tokens.php

您将通过此方法得到的注释包括(来自php.net站点):

PHP5中的T_COMMENT : //或#和/* */

希望能帮上忙!

票数 4
EN

Stack Overflow用户

发布于 2011-06-11 06:49:34

要使注释成为文档,需要从/**开始,而不是使用标准的多行注释。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6314539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档