8000 Add public typed properties autowiring · symfony/symfony-docs@3451964 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3451964

Browse files
committed
Add public typed properties autowiring
1 parent 383a413 commit 3451964

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
@@ -295,6 +295,8 @@ The disadvantages of setter injection are:
295295
* You cannot be sure the setter will be called and so you need to add checks
296296
that any required dependencies are injected.
297297

298+
.. _property-injection:
299+
298300
Property Injection
299301
------------------
300302

0 commit comments

Comments
 (0)
0