Categories
PHP

Using annotations in PHP with Doctrine Annotation Reader

Ever since I have seen the use of Annotations in Symfony2 framework, I have been looking for a way to use them in my own projects as well. After looking into the Symfony2’s source code, I noticed that they are indirectly using Doctrine’s Annotation Reader. Reading annotation from your own class is pretty easy. PHP includes the docblocks in the different reflection objects. You just have to read the docblock and parse it. Doctrine does it pretty nicely. Let’s see an example!

To get started, you first need to install doctrine-common package. I recommend you install using composer. Here’s my composer.json for the project:

Now, we need to define the annotation classes. Every annotation you use should have a class with the same name. Doctrine Annotation Reader will map those annotations with these classes and pass the values to the public properties of these classes.

In our example, we shall use one annotation class named – “AnnotatedDescription”. It can be directly passed a value, like:

or in an associative fashion –

We shall allow two keys – “desc” and “type” in these example. But feel free to add your own. Again, in real life, I would declare separate classes for capturing single value and key-value pairs.

Lets declare the AnnotatedDescription class in annotations.php:

Note that, the $value property is used for annotations which accept just a value. For key value pairs, we must declare properties with the key names. You *must* annotate the classes with @Annotation to let Doctrine’s Annotation Reader know that this class should be used for annotations mapping.

Now, lets see the parser file (parse_annotations.php):

The code is pretty much self explanatory. We instantiate “Doctrine\Common\Annotations\AnnotationReader” and use it’s different methods to get the annotations. You might want to study the source code of “Doctrine\Common\Annotations” namespace for even detailed usages.

Note that we must pass reflection objects for the class, properties and methods (eg. ReflectionClass, ReflectionObject, ReflectionProperty, ReflectionMethod). For the properties , instead of directly constructing ReflectionProperty object, we can use getProperty() on a ReflectionObject object in runtime.

4 replies on “Using annotations in PHP with Doctrine Annotation Reader”

Not Running For Doctrine Annotations Latest Version !

Not Included Annotation Schema For @AnnotatedDescription

Automatic Annotation Reader seems to be useful, but the syntax isn’t any friendly.

Instead of using the function like call @AnnotatedDescription(….), There should have been simpler syntax

Like the one used in different languages

@param – for parameters

@author – for authors

@return for the return type specification.

 

This is pretty cool than the one with a function call like thing.

Thanks for sharing!

I think parse_annotations.php:67 should be $methodAnnotations instead of $propertyAnnotations

Comments are closed.