我最近熟悉Reflection
,並且一直在試驗它,特別是getDocComment()
,但它似乎只支持/** */
註釋塊。PHP反射;提取非塊註釋
/** foobar */
class MyClass{}
$refl = new ReflectionClass('MyClass');
// produces /** foobar */
echo $refl->getDocComment();
-Versus-
# 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]
)
關於我的編輯;它仍然不可行。 Microtime對Zend_Controller_Front類文件(* 1007行,3497個令牌*)進行了測試,平均需要「0.010729945898056」(在我的個人開發箱*上有1000次迭代) – Dan 2011-06-11 07:44:27
你是否明白這一點?想知道你是否找到了更好的方法。 – dader 2012-11-03 14:35:11
@dader不,對給定文件的直接文本解析可能會產生更好的性能,但是您需要理解結果。 – Dan 2012-11-13 18:27:53