我最近已经熟悉了Reflection,并且一直在试验它,特别是getDocComment(),但是它似乎只支持/** */评论块。
/** foobar */
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces /** foobar */
echo $refl->getDocComment();-对抗.
# foobar
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces nothing
echo $refl->getDocComment();难道不可能在不诉诸任何形式的file_get_contents(__FILE__)胡说八道的情况下捕捉到这一点吗?
根据dader51的回答,我认为我最好的方法是遵循以下思路:
// 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);产出:
Array
(
[4] => #[annotation]
[8] => #[another annotation]
)发布于 2011-06-11 06:52:43
与常规注释相比,DocComments通过说明如何使用类来区别自己,这些注释可以帮助开发人员阅读代码。这也是为什么方法不被称为getComment()的原因。
当然,这都是文本解析,而且有人在docComments中做了一个选择--总是这些多行注释,但这个选择显然已经做出了,而阅读常规注释并不属于反射类别。
发布于 2011-06-11 07:09:01
几天前我正试着做一个你,这就是我的诀窍。您只需使用php内部令牌程序( http://www.php.net/manual/en/function.token-get-all.php ),然后遍历返回的数组,只选择注释,下面是一个示例代码:
$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 : //或#和/* */
希望能帮上忙!
发布于 2011-06-11 06:49:34
要使注释成为文档,需要从/**开始,而不是使用标准的多行注释。
https://stackoverflow.com/questions/6314539
复制相似问题