index.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. We do not recommend using this library in new projects and encourage authors
  9. of downstream libraries to offer support for attributes as an alternative to
  10. Doctrine Annotations.
  11. Have a look at [our blog](https://www.doctrine-project.org/2022/11/04/annotations-to-attributes.html)
  12. to learn more.
  13. Introduction
  14. ============
  15. Doctrine Annotations allows to implement custom annotation
  16. functionality for PHP classes and functions.
  17. .. code-block:: php
  18. class Foo
  19. {
  20. /**
  21. * @MyAnnotation(myProperty="value")
  22. */
  23. private $bar;
  24. }
  25. Annotations aren't implemented in PHP itself which is why this component
  26. offers a way to use the PHP doc-blocks as a place for the well known
  27. annotation syntax using the ``@`` char.
  28. Annotations in Doctrine are used for the ORM configuration to build the
  29. class mapping, but it can be used in other projects for other purposes
  30. too.
  31. Installation
  32. ============
  33. You can install the Annotation component with composer:
  34. .. code-block::
  35. $ composer require doctrine/annotations
  36. Create an annotation class
  37. ==========================
  38. An annotation class is a representation of the later used annotation
  39. configuration in classes. The annotation class of the previous example
  40. looks like this:
  41. .. code-block:: php
  42. /**
  43. * @Annotation
  44. */
  45. final class MyAnnotation
  46. {
  47. public $myProperty;
  48. }
  49. The annotation class is declared as an annotation by ``@Annotation``.
  50. :ref:`Read more about custom annotations. <custom>`
  51. Reading annotations
  52. ===================
  53. The access to the annotations happens by reflection of the class or function
  54. containing them. There are multiple reader-classes implementing the
  55. ``Doctrine\Common\Annotations\Reader`` interface, that can access the
  56. annotations of a class. A common one is
  57. ``Doctrine\Common\Annotations\AnnotationReader``:
  58. .. code-block:: php
  59. use Doctrine\Common\Annotations\AnnotationReader;
  60. $reflectionClass = new ReflectionClass(Foo::class);
  61. $property = $reflectionClass->getProperty('bar');
  62. $reader = new AnnotationReader();
  63. $myAnnotation = $reader->getPropertyAnnotation(
  64. $property,
  65. MyAnnotation::class
  66. );
  67. echo $myAnnotation->myProperty; // result: "value"
  68. A reader has multiple methods to access the annotations of a class or
  69. function.
  70. :ref:`Read more about handling annotations. <annotations>`
  71. IDE Support
  72. -----------
  73. Some IDEs already provide support for annotations:
  74. - Eclipse via the `Symfony2 Plugin <https://github.com/pulse00/Symfony-2-Eclipse-Plugin>`_
  75. - 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>`_
  76. .. _Read more about handling annotations.: annotations
  77. .. _Read more about custom annotations.: custom