Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf8d3457

Browse files
[DI] Add section about getter injection
1 parentfefc2f2 commitf8d3457

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

‎service_container/injection_types.rst‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,86 @@ 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+
<containerxmlns="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+
<serviceid="app.newsletter_manager"class="AppBundle\Mail\NewsletterManager">
225+
<gettername="getMailer"type="service"id="mailer" />
226+
<gettername="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 disadvantage of getter injection is:
258+
259+
* By using inheritance to override methods, it doesn't work with final classes
260+
and requires such getters to be made protected or public.
261+
182262
Property Injection
183263
------------------
184264

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp