From 2d32d662dc4ab2db8ea87a6f26bf0b67f264422e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 29 Dec 2016 19:08:24 +0100 Subject: [PATCH 1/2] [DI] Add section about getter injection --- service_container/injection_types.rst | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/service_container/injection_types.rst b/service_container/injection_types.rst index 7be7c7e41bd..214d50afc80 100644 --- a/service_container/injection_types.rst +++ b/service_container/injection_types.rst @@ -179,6 +179,89 @@ 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. +Getter Injection +---------------- + + .. versionadded:: 3.3 + Getter Injection was introduced in Symfony 3.3. + +Another possible injection point into a class is by overriding a getter method +to make it return the dependency:: + + // ... + abstract class NewsletterManager + { + abstract protected function getMailer(): MailerInterface; + + protected function getLogger(): LoggerInterface + { + return new NullLogger(); + } + + // ... + } + +.. configuration-block:: + + .. code-block:: yaml + + services: + # ... + + app.newsletter_manager: + class: AppBundle\Mail\NewsletterManager + getters: + getMailer: '@mailer' + getLogger: '@logger' + + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: php + + use AppBundle\Mail\NewsletterManager; + use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\DependencyInjection\Reference; + + // ... + $container->register('app.newsletter_manager', NewsletterManager::class) + ->addOverriddenGetter('getMailer', new Reference('mailer')) + ->addOverriddenGetter('getLogger', new Reference('logger')) + ; + +This time the advantages are: + +* The dependency can be created lazily - ie only when it is actually needed. + +* It works well with both optional and required dependencies: either provide + a default implementation for optional ones, throw an exception or make the + getter abstract for required ones. + +* You can be sure that the dependency will not change during the object's + lifetime. + +* It works well with class hierarchies since you can also override getters of + parent classes. + +The disadvantage of getter injection is: + +* By using inheritance to override methods, it doesn't work with final classes + and requires such getters to be made protected or public. + Property Injection ------------------ From f5d13b64f9c8e8708a84283846acd9bc295649b7 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 1 Feb 2017 17:19:26 +0100 Subject: [PATCH 2/2] Add a note about being an experimental feature --- service_container/injection_types.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/service_container/injection_types.rst b/service_container/injection_types.rst index 214d50afc80..4170a989a81 100644 --- a/service_container/injection_types.rst +++ b/service_container/injection_types.rst @@ -182,8 +182,13 @@ The disadvantages of setter injection are: Getter Injection ---------------- - .. versionadded:: 3.3 - Getter Injection was introduced in Symfony 3.3. +.. versionadded:: 3.3 + Getter Injection was introduced in Symfony 3.3. + +.. caution:: + + This feature is marked as **experimental**. The next Symfony versions could + change its behavior or even remove this feature entirely. Another possible injection point into a class is by overriding a getter method to make it return the dependency::