如何找到在PHP5对象的注解? [英] How to find annotations in a PHP5 object?

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

问题描述

我希望能够实现我的PHP5对象的自定义注释,我想了解整个过程的工作方式建设自己的解析器。

要启动,不过,我需要知道如何找到注解。

有没有办法,我缺少反思的方法,或者是有另一种方式?

例如,我希望能找到一类以下注释:

  / **
 * @MyParam:myvalue的
 * /


解决方案

您可以做到这一点使用<一个href=\"http://www.php.net/manual/en/reflectionclass.getdoccomment.php\"><$c$c>ReflectionClass::getDocComment,例如:

 函数getClassAnnotations($类)
{
    $ R =新ReflectionClass($类);
    $文档= $ R-&GT; getDocComment();
    preg_match_all('#@(*)\\ N#秒。?',$ DOC,$注解);
    返回$注解[1];
}

现场演示: HTTP://$c$cpad.viper-7.com/u8bFT4

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

解决方案

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];
}

Live demo: http://codepad.viper-7.com/u8bFT4

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

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