Skip to content

Commit 136f195

Browse files
committed
minor #13851 Add public typed properties autowiring (l-vo)
This PR was merged into the 5.1 branch. Discussion ---------- Add public typed properties autowiring https://symfony.com/blog/new-in-symfony-5-1-autowire-public-typed-properties and implemented in symfony/symfony#34769 Commits ------- 3451964 Add public typed properties autowiring
2 parents c55baf4 + 3451964 commit 136f195

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

service_container/autowiring.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ You wire up the difficult arguments, Symfony takes care of the rest.
530530

531531
.. _autowiring-calls:
532532

533-
Autowiring other Methods (e.g. Setters)
534-
---------------------------------------
533+
Autowiring other Methods (e.g. Setters and Public Typed Properties)
534+
-------------------------------------------------------------------
535535

536536
When autowiring is enabled for a service, you can *also* configure the container
537537
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
562562
above it, autowiring each argument. If you need to manually wire some of the arguments
563563
to a method, you can always explicitly :doc:`configure the method call </service_container/calls>`.
564564

565+
Despite property injection has some :ref:`drawbacks <property-injection>`, autowiring with ``@required`` annotation
566+
can also be applied to public typed properties::
567+
568+
namespace App\Util;
569+
570+
class Rot13Transformer
571+
{
572+
/** @required */
573+
public LoggerInterface $logger;
574+
575+
public function transform($value)
576+
{
577+
$this->logger->info('Transforming '.$value);
578+
// ...
579+
}
580+
}
581+
582+
.. versionadded:: 5.1
583+
584+
Public typed properties autowiring was introduced in Symfony 5.1.
585+
565586
Autowiring Controller Action Methods
566587
------------------------------------
567588

service_container/injection_types.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ The disadvantages of setter injection are:
298298
* You cannot be sure the setter will be called and so you need to add checks
299299
that any required dependencies are injected.
300300

301+
.. _property-injection:
302+
301303
Property Injection
302304
------------------
303305

0 commit comments

Comments
 (0)