@@ -544,50 +544,107 @@ Autowiring other Methods (e.g. Setters and Public Typed Properties)
544544
545545When autowiring is enabled for a service, you can *also * configure the container
546546to call methods on your class when it's instantiated. For example, suppose you want
547- to inject the ``logger `` service, and decide to use setter-injection::
547+ to inject the ``logger `` service, and decide to use setter-injection:
548548
549- // src/Util/Rot13Transformer.php
550- namespace App\Util;
549+ .. configuration-block ::
551550
552- class Rot13Transformer
553- {
554- private $logger;
551+ .. code-block :: php-annotations
555552
556- /**
557- * @required
558- */
559- public function setLogger(LoggerInterface $logger)
553+ // src/Util/Rot13Transformer.php
554+ namespace App\Util;
555+
556+ class Rot13Transformer
560557 {
561- $this->logger = $logger;
558+ private $logger;
559+
560+ /**
561+ * @required
562+ */
563+ public function setLogger(LoggerInterface $logger)
564+ {
565+ $this->logger = $logger;
566+ }
567+
568+ public function transform($value)
569+ {
570+ $this->logger->info('Transforming '.$value);
571+ // ...
572+ }
562573 }
563574
564- public function transform($value)
575+ .. code-block :: php-attributes
576+
577+ // src/Util/Rot13Transformer.php
578+ namespace App\Util;
579+
580+ use Symfony\Contracts\Service\Attribute\Required;
581+
582+ class Rot13Transformer
565583 {
566- $this->logger->info('Transforming '.$value);
567- // ...
584+ private $logger;
585+
586+ #[Required]
587+ public function setLogger(LoggerInterface $logger)
588+ {
589+ $this->logger = $logger;
590+ }
591+
592+ public function transform($value)
593+ {
594+ $this->logger->info('Transforming '.$value);
595+ // ...
596+ }
568597 }
569- }
570598
571- Autowiring will automatically call *any * method with the ``@required `` annotation
599+ Autowiring will automatically call *any * method with the ``#[Required] `` attribute
572600above it, autowiring each argument. If you need to manually wire some of the arguments
573601to a method, you can always explicitly :doc: `configure the method call </service_container/calls >`.
574602
575- Despite property injection has some :ref: ` drawbacks < property-injection >`, autowiring with `` @required `` annotation
576- can also be applied to public typed properties::
603+ If you need to stay compatible with PHP 7 and thus cannot use attributes, you can use
604+ the `` @required `` annotation instead.
577605
578- namespace App\Util;
606+ .. versionadded :: 5.2
579607
580- class Rot13Transformer
581- {
582- /** @required */
583- public LoggerInterface $logger;
608+ The ``#[Required] `` attribute was introduced in Symfony 5.2.
584609
585- public function transform($value)
610+ Despite property injection has some :ref: `drawbacks <property-injection >`, autowiring with ``#[Required] ``
611+ or ``@required `` can also be applied to public typed properties::
612+
613+ .. configuration-block ::
614+
615+ .. code-block :: php-annotations
616+
617+ namespace App\Util;
618+
619+ class Rot13Transformer
586620 {
587- $this->logger->info('Transforming '.$value);
588- // ...
621+ /** @required */
622+ public LoggerInterface $logger;
623+
624+ public function transform($value)
625+ {
626+ $this->logger->info('Transforming '.$value);
627+ // ...
628+ }
629+ }
630+
631+ .. code-block :: php-attributes
632+
633+ namespace App\Util;
634+
635+ use Symfony\Contracts\Service\Attribute\Required;
636+
637+ class Rot13Transformer
638+ {
639+ #[Required]
640+ public LoggerInterface $logger;
641+
642+ public function transform($value)
643+ {
644+ $this->logger->info('Transforming '.$value);
645+ // ...
646+ }
589647 }
590- }
591648
592649 .. versionadded :: 5.1
593650
0 commit comments