---
title: "Addendum PHP Reflection Annotations简介"
author: "bardo"
date: "2011-02-16T23:50:12Z"
tags: []
categories: ["编程语言"]
slug: "addendum-php-reflection-annotations"
draft: false
lastmod: "2011-02-16T23:53:19Z"
id: 908276
url: "https://bardo.iteye.com/blog/908276"
---

Addendum PHP Reflection Annotations 是一个PHP的开源，它的网址在：  [http://code.google.com/p/addendum/](http://code.google.com/p/addendum/)

        Addendum PHP Reflection Annotations是什么？如果你会JAVA，那么，你肯定了解 JAVA Reflection Annotations。 对于不懂JAVA的人，需要说明一下，它倒底是什么。很简单，它就是一个Reflection 。但这个Reflection 并不是一般的Reflection ，因为它操作的是Annotations。Annotations指的是什么？是代码中的文档与注解。

        PHP中的很好的Reflection 可用。但是，却没有Reflection Annotations ，因此，这一代码就让你能够使用这样的功能。

        这东西有什么用？用过JAVA Reflection Annotations肯定了解，它能帮助你省下大量的代码。原理是什么呢？因为，有了它，代码中的注解，也是代码了。并且，通过这一手段，能使你实现完全的松耦合。

        如果你用过PHP的EXPDO，那么，你肯定会发现，其中也有与此相类似的代码。这就是说，如果实现ORM，那么，它能够为你的ORM对象，进行一个前置的操作，比如，数据验证，如同JAVA 中ORM实现的那样。这也是Addendum最重要的使用场合。

        Addendum PHP Reflection Annotations 共有三个文件。 annotations.php 是主程序文件。  annotation\_parser.php 则是类型解析器文件。 doc\_comment.php 则是处理文档注解的。 

         使用Addendum 最关键的是，首先需要在类中添加“Annotations”模式的注解。比如以下的例子：

```
/** @Persistent */ 
class Person {    // some code }

/** @Secured(role = "admin", level = 2) */ 
class Administration {  // some code }

class RolesAllowed extends Annotation {}  
/** @RolesAllowed({'admin', 'web-editor'}) */ 
class CMS {  // some code } 
```

         Addendum 对函数的参数也能反射，当然，这也需要你给出符合格式的注解，如：

   

```
function addUser(/** unique */ $userName, /** integer */ userId)
{some code }
```

 

        由此可见，其功能是相当强大的。Addendum同时提供了类的生成器？可惜的是JAVA的JAR包。所以，无法了解更多的细节。精通JAVA的不妨可以测试一下它的生成器。更详细的文档，还是直接去看Addendum的项目主页吧。