index.rst 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. Introduction
  2. ============
  3. Doctrine Annotations allows to implement custom annotation
  4. functionality for PHP classes.
  5. .. code-block:: php
  6. class Foo
  7. {
  8. /**
  9. * @MyAnnotation(myProperty="value")
  10. */
  11. private $bar;
  12. }
  13. Annotations aren't implemented in PHP itself which is why this component
  14. offers a way to use the PHP doc-blocks as a place for the well known
  15. annotation syntax using the ``@`` char.
  16. Annotations in Doctrine are used for the ORM configuration to build the
  17. class mapping, but it can be used in other projects for other purposes
  18. too.
  19. Installation
  20. ============
  21. You can install the Annotation component with composer:
  22. .. code-block::
  23.   $ composer require doctrine/annotations
  24. Create an annotation class
  25. ==========================
  26. An annotation class is a representation of the later used annotation
  27. configuration in classes. The annotation class of the previous example
  28. looks like this:
  29. .. code-block:: php
  30. /**
  31. * @Annotation
  32. */
  33. final class MyAnnotation
  34. {
  35. public $myProperty;
  36. }
  37. The annotation class is declared as an annotation by ``@Annotation``.
  38. :ref:`Read more about custom annotations. <custom>`
  39. Reading annotations
  40. ===================
  41. The access to the annotations happens by reflection of the class
  42. containing them. There are multiple reader-classes implementing the
  43. ``Doctrine\Common\Annotations\Reader`` interface, that can access the
  44. annotations of a class. A common one is
  45. ``Doctrine\Common\Annotations\AnnotationReader``:
  46. .. code-block:: php
  47. use Doctrine\Common\Annotations\AnnotationReader;
  48. use Doctrine\Common\Annotations\AnnotationRegistry;
  49. // Deprecated and will be removed in 2.0 but currently needed
  50. AnnotationRegistry::registerLoader('class_exists');
  51. $reflectionClass = new ReflectionClass(Foo::class);
  52. $property = $reflectionClass->getProperty('bar');
  53. $reader = new AnnotationReader();
  54. $myAnnotation = $reader->getPropertyAnnotation($property, MyAnnotation::class);
  55. echo $myAnnotation->myProperty; // result: "value"
  56. Note that ``AnnotationRegistry::registerLoader('class_exists')`` only works
  57. if you already have an autoloader configured (i.e. composer autoloader).
  58. Otherwise, :ref:`please take a look to the other annotation autoload mechanisms <annotations>`.
  59. A reader has multiple methods to access the annotations of a class.
  60. :ref:`Read more about handling annotations. <annotations>`
  61. IDE Support
  62. -----------
  63. Some IDEs already provide support for annotations:
  64. - Eclipse via the `Symfony2 Plugin <http://symfony.dubture.com/>`_
  65. - PHPStorm via the `PHP Annotations Plugin <http://plugins.jetbrains.com/plugin/7320>`_ or the `Symfony2 Plugin <http://plugins.jetbrains.com/plugin/7219>`_
  66. .. _Read more about handling annotations.: annotations
  67. .. _Read more about custom annotations.: custom