Symfony News

New in Symfony 3.3: Configurable autowiring setters

Kévin Dunglas

Contributed by
Kévin Dunglas
in #20167.

Autowiring allows to register services in the container with minimal configuration. It automatically resolves the service dependencies based on the constructor's typehints, so it drastically reduces the amount of configuration required to define a service.

By default, the autowiring subsystem only injects dependencies in the constructor of the services. However, even if the constructor injection should always be preferred, sometimes it's convenient to inject dependencies through methods (usually a setter method).

That's why in Symfony 3.3, the autowiring subsystem allows to configure the setters to use thanks to the new values supported by the autowire option:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
services:
    app.manager.user:
        class: AppBundle\Manager\UserManager
        # to maintain backwards compatibility this value is
        # automatically transformed into "autowire: ['__construct']"
        autowire: true

        # autowire the constructor and all the setters
        autowire: ['__construct', 'set*']

        # autowire only the foo() and bar() methods
        autowire: ['foo', 'bar']

Be trained by Symfony experts - 2017-01-09 Paris - 2017-01-09 Paris - 2017-01-11 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook