如何在 PHP5 对象中查找注释? [英] How to find annotations in a PHP5 object?

查看:27
本文介绍了如何在 PHP5 对象中查找注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的 PHP5 对象中实现自定义注释,并且我想通过构建我自己的解析器来了解整个过程是如何工作的.

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.

不过,首先,我需要知道如何查找注释.

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
 */

推荐答案

您可以使用 ReflectionClass::getDocComment,例如:

You can do this using ReflectionClass::getDocComment, example:

function getClassAnnotations($class)
{       
    $r = new ReflectionClass($class);
    $doc = $r->getDocComment();
    preg_match_all('#@(.*?)\n#s', $doc, $annotations);
    return $annotations[1];
}

现场演示:http://codepad.viper-7.com/u8bFT4

这篇关于如何在 PHP5 对象中查找注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆