Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add public typed properties autowiring #13851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 </service_container/calls>`.

Despite property injection has some :ref:`drawbacks <property-injection>`, 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
------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions service_container/injection_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------

Expand Down