php 如何在 PHP5 对象中查找注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9742564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find annotations in a PHP5 object?
提问by johnnietheblack
I would like to be able to implement custom annotations in my PHP5 objects, and I'd like to learn how the whole process works by building my own parser.
我希望能够在我的 PHP5 对象中实现自定义注释,并且我想通过构建我自己的解析器来了解整个过程是如何工作的。
To start, though, I need to know how to FIND the annotations.
不过,首先,我需要知道如何查找注释。
Is there a Reflection method that I am missing, or is there another way?
是否有我缺少的反射方法,还是有其他方法?
For example, I'd like to be able to find the following annotation in a class:
例如,我希望能够在类中找到以下注释:
/**
* @MyParam: myvalue
*/
回答by Robik
You can do this using ReflectionClass::getDocComment
, example:
您可以使用ReflectionClass::getDocComment
,例如:
function getClassAnnotations($class)
{
$r = new ReflectionClass($class);
$doc = $r->getDocComment();
preg_match_all('#@(.*?)\n#s', $doc, $annotations);
return $annotations[1];
}
Live demo: http://codepad.viper-7.com/u8bFT4
回答by Slawek
You can get comment block using getDocCommentReflection object method.
您可以使用getDocCommentReflection 对象方法获取注释块。
If you don't want to retrieve annotation by hand, you can use Zend Framework Reflectionor other existing solution
如果不想手动检索注释,可以使用Zend Framework Reflection或其他现有解决方案