Dan*_*ugg 1 php reflection comments phpdoc
我最近熟悉Reflection
并且一直在尝试它,特别是getDocComment()
,它似乎只支持/** */
注释块.
/** foobar */
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces /** foobar */
echo $refl->getDocComment();
Run Code Online (Sandbox Code Playgroud)
-与-
# foobar
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces nothing
echo $refl->getDocComment();
Run Code Online (Sandbox Code Playgroud)
如果不诉诸任何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);
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[4] => #[annotation]
[8] => #[another annotation]
)
Run Code Online (Sandbox Code Playgroud)
与可以帮助开发人员阅读代码的常规注释相比,DocComments通过说出如何使用类的方式来区分自己.这也是为什么不调用该方法的原因getComment()
.
当然,这是所有文本解析,有人只是在docComments中做出选择总是这些多行注释,但显然已经做出了选择,并且阅读常规注释不是反射类别中的内容.