index.rst 3.2 KB

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