@@ -544,50 +544,107 @@ Autowiring other Methods (e.g. Setters and Public Typed Properties)
544
544
545
545
When autowiring is enabled for a service, you can *also * configure the container
546
546
to 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:
548
548
549
- // src/Util/Rot13Transformer.php
550
- namespace App\Util;
549
+ .. configuration-block ::
551
550
552
- class Rot13Transformer
553
- {
554
- private $logger;
551
+ .. code-block :: php-annotations
555
552
556
- /**
557
- * @required
558
- */
559
- public function setLogger(LoggerInterface $logger)
553
+ // src/Util/Rot13Transformer.php
554
+ namespace App\Util;
555
+
556
+ class Rot13Transformer
560
557
{
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
+ }
562
573
}
563
574
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
565
583
{
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
+ }
568
597
}
569
- }
570
598
571
- Autowiring will automatically call *any * method with the ``@required `` annotation
599
+ Autowiring will automatically call *any * method with the ``#[Required] `` attribute
572
600
above it, autowiring each argument. If you need to manually wire some of the arguments
573
601
to a method, you can always explicitly :doc: `configure the method call </service_container/calls >`.
574
602
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.
577
605
578
- namespace App\Util;
606
+ .. versionadded :: 5.2
579
607
580
- class Rot13Transformer
581
- {
582
- /** @required */
583
- public LoggerInterface $logger;
608
+ The ``#[Required] `` attribute was introduced in Symfony 5.2.
584
609
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
586
620
{
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
+ }
589
647
}
590
- }
591
648
592
649
.. versionadded :: 5.1
593
650
0 commit comments