index.rst 2.3 KB

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