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:
1 2 3 |
/** *@AnnotatedDescription("The value to be passed") */ |
or in an associative fashion –
1 2 3 |
/** * @AnnotatedDescription(desc ="value for desc key", type="value for type key") */ |
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.