Doctrine Annotations ==================== The Doctrine Common annotations library was born from a need in the `Doctrine2 ORM `_ to allow the mapping information to be specified as metadata embedded in the class files, on properties and methods. The library is independent and can be used in your own libraries to implement doc block annotations. Introduction ------------ There are several different approaches to handling annotations in PHP. Doctrine Annotations maps docblock annotations to PHP classes. Because not all docblock annotations are used for metadata purposes a filter is applied to ignore or skip classes that are not Doctrine annotations. Take a look at the following code snippet: .. code-block :: php `_ for autoloading. This is why Doctrine Annotations uses its own autoloading mechanism through a global registry. If you are wondering about the annotation registry being global, there is no other way to solve the architectural problems of autoloading annotation classes in a straightforward fashion. Additionally if you think about PHP autoloading then you recognize it is a global as well. To anticipate the configuration section, making the above PHP class work with Doctrine Annotations requires this setup: .. code-block :: php setDefaultAnnotationNamespace('MyCompany\Annotations\\'); Now it can look something like: .. code-block :: php setEnabledPhpImports(false); Annotation Classes ------------------ If you want to define your own annotations you just have to group them in a namespace and register this namespace in the AnnotationRegistry. Annotation classes have to contain a class-level docblock with the text @Annotation: .. code-block :: php foo = $values['foo']; } } /** * @Annotation * * Some Annotation without a constructor */ class Foo { public $bar; } Annotation Target ----------------- ``@Target`` indicates the kinds of class element to which an annotation type is applicable. Then you could define one or more targets : - ``CLASS`` Allowed in the class docblock - ``PROPERTY`` Allowed in the property docblock - ``METHOD`` Allowed in the method docblock - ``ALL`` Allowed in the class, property and method docblock - ``ANNOTATION`` Allowed inside other annotations If the annotations is not allowed in the current context you got an ``AnnotationException`` .. code-block :: php */ public $arrayOfIntegers; /** @var array */ public $arrayOfAnnotations; } /** * @Annotation * @Target({"METHOD","PROPERTY"}) * @Attributes({ * @Attribute("stringProperty", type = "string"), * @Attribute("annotProperty", type = "SomeAnnotationClass"), * }) */ class Foo { public function __construct(array $values) { $this->stringProperty = $values['stringProperty']; $this->annotProperty = $values['annotProperty']; } // some code } Constants ----------- The use of constants and class constants are available on the annotations parser. The following usage are allowed : .. code-block :: php getClassAnnotations($reflClass); foreach ($classAnnotations AS $annot) { if ($annot instanceof \MyCompany\Annotations\Foo) { echo $annot->bar; // prints "foo"; } else if ($annot instanceof \MyCompany\Annotations\Bar) { echo $annot->foo; // prints "bar"; } } You have a complete API for retrieving annotation class instances from a class, property or method docblock: - ``AnnotationReader#getClassAnnotations(ReflectionClass $class)`` - ``AnnotationReader#getClassAnnotation(ReflectionClass $class, $annotation)`` - ``AnnotationReader#getPropertyAnnotations(ReflectionProperty $property)`` - ``AnnotationReader#getPropertyAnnotation(ReflectionProperty $property, $annotation)`` - ``AnnotationReader#getMethodAnnotations(ReflectionMethod $method)`` - ``AnnotationReader#getMethodAnnotation(ReflectionMethod $method, $annotation)`` IDE Support ----------- Some IDEs already provide support for annotations: - Eclipse via the `Symfony2 Plugin `_