Skip to content

Commit f9144d5

Browse files
[DI] Add section about getter injection
1 parent fefc2f2 commit f9144d5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

service_container/injection_types.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,88 @@ The disadvantages of setter injection are:
179179
* You cannot be sure the setter will be called and so you need to add checks
180180
that any required dependencies are injected.
181181

182+
Getter Injection
183+
----------------
184+
185+
Another possible injection point into a class is by overriding a getter method
186+
to make it return the dependency::
187+
188+
// ...
189+
abstract class NewsletterManager
190+
{
191+
abstract protected function getMailer(): MailerInterface;
192+
193+
protected function getLogger(): LoggerInterface
194+
{
195+
return new NullLogger();
196+
}
197+
198+
// ...
199+
}
200+
201+
.. configuration-block::
202+
203+
.. code-block:: yaml
204+
205+
services:
206+
# ...
207+
208+
app.newsletter_manager:
209+
class: AppBundle\Mail\NewsletterManager
210+
getters:
211+
getMailer: '@mailer'
212+
getLogger: '@logger'
213+
214+
.. code-block:: xml
215+
216+
<?xml version="1.0" encoding="UTF-8" ?>
217+
<container xmlns="http://symfony.com/schema/dic/services"
218+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
219+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
220+
221+
<services>
222+
<!-- ... -->
223+
224+
<service id="app.newsletter_manager" class="AppBundle\Mail\NewsletterManager">
225+
<getter name="getMailer" type="service" id="mailer" />
226+
<getter name="getLogger" type="service" id="logger" />
227+
</service>
228+
</services>
229+
</container>
230+
231+
.. code-block:: php
232+
233+
use AppBundle\Mail\NewsletterManager;
234+
use Symfony\Component\DependencyInjection\Definition;
235+
use Symfony\Component\DependencyInjection\Reference;
236+
237+
// ...
238+
$container->register('app.newsletter_manager', NewsletterManager::class)
239+
->addOverriddenGetter('getMailer', new Reference('mailer'))
240+
->addOverriddenGetter('getLogger', new Reference('logger'))
241+
;
242+
243+
This time the advantages are:
244+
245+
* The dependency can be created lazily - ie only when it is actually needed.
246+
247+
* It works well with both optional and required dependencies: either provide
248+
a default implementation for optional ones, or make the getter abstract for
249+
required ones.
250+
251+
* You can be sure that the dependency will not change during the object's
252+
lifetime.
253+
254+
* It works well with class hierarchies since you can also override getters of
255+
parent classes.
256+
257+
The disadvantages of getter injection are:
258+
259+
* You must call the getter everytime you need the dependency internally.
260+
261+
* By using inheritance to override methods, it doesn't work with final classes
262+
and requires such getters to be made protected or public.
263+
182264
Property Injection
183265
------------------
184266

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy