index.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Introduction
  2. ============
  3. Doctrine Annotations allows to implement custom annotation
  4. functionality for PHP classes and functions.
  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 or function
  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(
  55. $property,
  56. MyAnnotation::class
  57. );
  58. echo $myAnnotation->myProperty; // result: "value"
  59. Note that ``AnnotationRegistry::registerLoader('class_exists')`` only works
  60. if you already have an autoloader configured (i.e. composer autoloader).
  61. Otherwise, :ref:`please take a look to the other annotation autoload mechanisms <annotations>`.
  62. A reader has multiple methods to access the annotations of a class or
  63. function.
  64. :ref:`Read more about handling annotations. <annotations>`
  65. IDE Support
  66. -----------
  67. Some IDEs already provide support for annotations:
  68. - Eclipse via the `Symfony2 Plugin <http://symfony.dubture.com/>`_
  69. - PhpStorm via the `PHP Annotations Plugin <https://plugins.jetbrains.com/plugin/7320-php-annotations>`_ or the `Symfony Plugin <https://plugins.jetbrains.com/plugin/7219-symfony-support>`_
  70. .. _Read more about handling annotations.: annotations
  71. .. _Read more about custom annotations.: custom