|
| 1 | +--- |
| 2 | +id: doctrine-annotations-attributes |
| 3 | +title: Doctrine annotations VS PHP8 attributes |
| 4 | +sidebar_label: Annotations VS Attributes |
| 5 | +--- |
| 6 | + |
| 7 | +GraphQLite is heavily relying on the concept of annotations (also called attributes in PHP 8+). |
| 8 | + |
| 9 | +## Doctrine annotations |
| 10 | + |
| 11 | +<div class="alert alert-warning"><strong>Deprecated!</strong> Doctrine annotations are deprecated in favor of native PHP 8 attributes. Support will be dropped in GraphQLite 5.0</div> |
| 12 | + |
| 13 | +Historically, attributes were not available in PHP and PHP developers had to "trick" PHP to get annotation support. |
| 14 | +This was the purpose of the [doctrine/annotation](https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/index.html) library. |
| 15 | + |
| 16 | +Using Doctrine annotations, you write annotations in your docblocks: |
| 17 | + |
| 18 | +```php |
| 19 | +use TheCodingMachine\GraphQLite\Annotations\Type; |
| 20 | + |
| 21 | +/** |
| 22 | + * @Type |
| 23 | + */ |
| 24 | +class MyType |
| 25 | +{ |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Please note that: |
| 30 | + |
| 31 | +- The annotation is added in a **docblock** (a comment starting with "`/**`") |
| 32 | +- The `Type` part is actually a class. It must be declared in the `use` statements at the top of your file. |
| 33 | + |
| 34 | + |
| 35 | +<div class="alert alert-info"><strong>Heads up!</strong> |
| 36 | +Some IDEs provide support for Doctrine annotations: |
| 37 | + |
| 38 | +<ul> |
| 39 | +<li>PhpStorm via the <a href="PHP Annotations Plugin">https://plugins.jetbrains.com/plugin/7320-php-annotations</a></li> |
| 40 | +<li>Eclipse via the <a href="Symfony2 Plugin">https://marketplace.eclipse.org/content/symfony-plugin</a></li> |
| 41 | +<li>Netbeans has native support</li> |
| 42 | +</ul> |
| 43 | + |
| 44 | +We strongly recommend using an IDE that has Doctrine annotations support. |
| 45 | +</div> |
| 46 | + |
| 47 | +## PHP 8 attributes |
| 48 | + |
| 49 | +Starting with PHP 8, PHP got native annotations support. They are actually called "attributes" in the PHP world. |
| 50 | + |
| 51 | +The same code can be written this way: |
| 52 | + |
| 53 | +```php |
| 54 | +use TheCodingMachine\GraphQLite\Annotations\Type; |
| 55 | + |
| 56 | +#[Type] |
| 57 | +class MyType |
| 58 | +{ |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +GraphQLite v4.1+ has support for PHP 8 attributes. |
| 63 | + |
| 64 | +The Doctrine annotation class and the PHP 8 attribute class is **the same** (so you will be using the same `use` statement at the top of your file). |
| 65 | + |
| 66 | +They support the same attributes too. |
| 67 | + |
| 68 | +A few notable differences: |
| 69 | + |
| 70 | +- PHP 8 attributes do not support nested attributes (unlike Doctrine annotations). This means there is no equivalent to the `annotations` attribute of `@MagicField` and `@SourceField`. |
| 71 | +- PHP 8 attributes can be written at the parameter level. Any attribute targeting a "parameter" must be written at the parameter level. |
| 72 | + |
| 73 | +Let's take an example with the [`#Autowire` attribute](autowiring.md): |
| 74 | + |
| 75 | +**PHP 7+** |
| 76 | +``` |
| 77 | +/** |
| 78 | + * @Field |
| 79 | + * @Autowire(for="$productRepository") |
| 80 | + */ |
| 81 | +public function getProduct(ProductRepository $productRepository) : Product { |
| 82 | + //... |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +**PHP 8** |
| 87 | +``` |
| 88 | +#[Field] |
| 89 | +public function getProduct(#[Autowire] ProductRepository $productRepository) : Product { |
| 90 | + //... |
| 91 | +} |
| 92 | +``` |
| 93 | + |
0 commit comments