From 345196443240574da475b46aeb6afeff150a4160 Mon Sep 17 00:00:00 2001 From: Laurent VOULLEMIER Date: Mon, 15 Jun 2020 22:55:30 +0200 Subject: [PATCH] Add public typed properties autowiring --- service_container/autowiring.rst | 25 +++++++++++++++++++++++-- service_container/injection_types.rst | 2 ++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/service_container/autowiring.rst b/service_container/autowiring.rst index f3fd75d8b54..6c8957f1bb6 100644 --- a/service_container/autowiring.rst +++ b/service_container/autowiring.rst @@ -530,8 +530,8 @@ You wire up the difficult arguments, Symfony takes care of the rest. .. _autowiring-calls: -Autowiring other Methods (e.g. Setters) ---------------------------------------- +Autowiring other Methods (e.g. Setters and Public Typed Properties) +------------------------------------------------------------------- When autowiring is enabled for a service, you can *also* configure the container to call methods on your class when it's instantiated. For example, suppose you want @@ -562,6 +562,27 @@ Autowiring will automatically call *any* method with the ``@required`` annotatio above it, autowiring each argument. If you need to manually wire some of the arguments to a method, you can always explicitly :doc:`configure the method call `. +Despite property injection has some :ref:`drawbacks `, autowiring with ``@required`` annotation +can also be applied to public typed properties:: + + namespace App\Util; + + class Rot13Transformer + { + /** @required */ + public LoggerInterface $logger; + + public function transform($value) + { + $this->logger->info('Transforming '.$value); + // ... + } + } + +.. versionadded:: 5.1 + + Public typed properties autowiring was introduced in Symfony 5.1. + Autowiring Controller Action Methods ------------------------------------ diff --git a/service_container/injection_types.rst b/service_container/injection_types.rst index b0b59ee8ea8..df71fa9cfdb 100644 --- a/service_container/injection_types.rst +++ b/service_container/injection_types.rst @@ -295,6 +295,8 @@ The disadvantages of setter injection are: * You cannot be sure the setter will be called and so you need to add checks that any required dependencies are injected. +.. _property-injection: + Property Injection ------------------