@@ -184,34 +184,31 @@ a parent for a service.
184
184
# ...
185
185
newsletter_manager.class : NewsletterManager
186
186
greeting_card_manager.class : GreetingCardManager
187
- mail_manager.class : MailManager
188
187
services :
189
188
my_mailer :
190
189
# ...
191
190
my_email_formatter :
192
191
# ...
193
192
mail_manager :
194
- class : " %mail_manager.class%"
195
193
abstract : true
196
194
calls :
197
195
- [ setMailer, [ @my_mailer ] ]
198
196
- [ setEmailFormatter, [ @my_email_formatter] ]
199
-
197
+
200
198
newsletter_manager :
201
199
class : " %newsletter_manager.class%"
202
200
parent : mail_manager
203
-
201
+
204
202
greeting_card_manager :
205
203
class : " %greeting_card_manager.class%"
206
204
parent : mail_manager
207
-
205
+
208
206
.. code-block :: xml
209
207
210
208
<parameters >
211
209
<!-- ... -->
212
210
<parameter key =" newsletter_manager.class" >NewsletterManager</parameter >
213
211
<parameter key =" greeting_card_manager.class" >GreetingCardManager</parameter >
214
- <parameter key =" mail_manager.class" >MailManager</parameter >
215
212
</parameters >
216
213
217
214
<services >
@@ -221,7 +218,7 @@ a parent for a service.
221
218
<service id =" my_email_formatter" ...>
222
219
<!-- ... -->
223
220
</service >
224
- <service id =" mail_manager" class = " %mail_manager.class% " abstract =" true" >
221
+ <service id =" mail_manager" abstract =" true" >
225
222
<call method =" setMailer" >
226
223
<argument type =" service" id =" my_mailer" />
227
224
</call >
@@ -242,12 +239,10 @@ a parent for a service.
242
239
// ...
243
240
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
244
241
$container->setParameter('greeting_card_manager.class', 'GreetingCardManager');
245
- $container->setParameter('mail_manager.class', 'MailManager');
246
242
247
243
$container->setDefinition('my_mailer', ...);
248
244
$container->setDefinition('my_email_formatter', ...);
249
245
$container->setDefinition('mail_manager', new Definition(
250
- '%mail_manager.class%'
251
246
))->setAbstract(
252
247
true
253
248
)->addMethodCall('setMailer', array(
@@ -279,16 +274,15 @@ when the child services are instantiated.
279
274
defined on the ``mail_manager `` service will not be executed when the
280
275
child services are instantiated.
281
276
282
- The parent class is abstract as it should not be directly instantiated. Setting
283
- it to abstract in the config file as has been done above will mean that it
284
- can only be used as a parent service and cannot be used directly as a service
285
- to inject and will be removed at compile time. In other words, it exists merely
286
- as a "template" that other services can use.
277
+ The parent service is abstract as it should not be directly retrieved from the
278
+ container or passed into another service. It exists merely as a "template" that
279
+ other services can use. This is why it has no ``class `` configured which would
280
+ cause an exception to be raised for a non-abstract service.
287
281
288
282
.. note ::
289
283
290
- In order for parent dependencies to resolve, the ``ContainerBuilder `` must
291
- first be compiled. See :doc: `/components/dependency_injection/compilation `
284
+ In order for parent dependencies to resolve, the ``ContainerBuilder `` must
285
+ first be compiled. See :doc: `/components/dependency_injection/compilation `
292
286
for more details.
293
287
294
288
Overriding Parent Dependencies
@@ -308,7 +302,6 @@ to the ``NewsletterManager`` class, the config would look like this:
308
302
# ...
309
303
newsletter_manager.class : NewsletterManager
310
304
greeting_card_manager.class : GreetingCardManager
311
- mail_manager.class : MailManager
312
305
services :
313
306
my_mailer :
314
307
# ...
@@ -317,29 +310,27 @@ to the ``NewsletterManager`` class, the config would look like this:
317
310
my_email_formatter :
318
311
# ...
319
312
mail_manager :
320
- class : " %mail_manager.class%"
321
313
abstract : true
322
314
calls :
323
315
- [ setMailer, [ @my_mailer ] ]
324
316
- [ setEmailFormatter, [ @my_email_formatter] ]
325
-
317
+
326
318
newsletter_manager :
327
319
class : " %newsletter_manager.class%"
328
320
parent : mail_manager
329
321
calls :
330
322
- [ setMailer, [ @my_alternative_mailer ] ]
331
-
323
+
332
324
greeting_card_manager :
333
325
class : " %greeting_card_manager.class%"
334
326
parent : mail_manager
335
-
327
+
336
328
.. code-block :: xml
337
329
338
330
<parameters >
339
331
<!-- ... -->
340
332
<parameter key =" newsletter_manager.class" >NewsletterManager</parameter >
341
333
<parameter key =" greeting_card_manager.class" >GreetingCardManager</parameter >
342
- <parameter key =" mail_manager.class" >MailManager</parameter >
343
334
</parameters >
344
335
345
336
<services >
@@ -352,7 +343,7 @@ to the ``NewsletterManager`` class, the config would look like this:
352
343
<service id =" my_email_formatter" ...>
353
344
<!-- ... -->
354
345
</service >
355
- <service id =" mail_manager" class = " %mail_manager.class% " abstract =" true" >
346
+ <service id =" mail_manager" abstract =" true" >
356
347
<call method =" setMailer" >
357
348
<argument type =" service" id =" my_mailer" />
358
349
</call >
@@ -377,13 +368,11 @@ to the ``NewsletterManager`` class, the config would look like this:
377
368
// ...
378
369
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
379
370
$container->setParameter('greeting_card_manager.class', 'GreetingCardManager');
380
- $container->setParameter('mail_manager.class', 'MailManager');
381
371
382
372
$container->setDefinition('my_mailer', ...);
383
373
$container->setDefinition('my_alternative_mailer', ...);
384
374
$container->setDefinition('my_email_formatter', ...);
385
375
$container->setDefinition('mail_manager', new Definition(
386
- '%mail_manager.class%'
387
376
))->setAbstract(
388
377
true
389
378
)->addMethodCall('setMailer', array(
@@ -442,30 +431,27 @@ If you had the following config:
442
431
parameters :
443
432
# ...
444
433
newsletter_manager.class : NewsletterManager
445
- mail_manager.class : MailManager
446
434
services :
447
435
my_filter :
448
436
# ...
449
437
another_filter :
450
438
# ...
451
439
mail_manager :
452
- class : " %mail_manager.class%"
453
440
abstract : true
454
441
calls :
455
442
- [ setFilter, [ @my_filter ] ]
456
-
443
+
457
444
newsletter_manager :
458
445
class : " %newsletter_manager.class%"
459
446
parent : mail_manager
460
447
calls :
461
448
- [ setFilter, [ @another_filter ] ]
462
-
449
+
463
450
.. code-block :: xml
464
451
465
452
<parameters >
466
453
<!-- ... -->
467
454
<parameter key =" newsletter_manager.class" >NewsletterManager</parameter >
468
- <parameter key =" mail_manager.class" >MailManager</parameter >
469
455
</parameters >
470
456
471
457
<services >
@@ -475,7 +461,7 @@ If you had the following config:
475
461
<service id =" another_filter" ...>
476
462
<!-- ... -->
477
463
</service >
478
- <service id =" mail_manager" class = " %mail_manager.class% " abstract =" true" >
464
+ <service id =" mail_manager" abstract =" true" >
479
465
<call method =" setFilter" >
480
466
<argument type =" service" id =" my_filter" />
481
467
</call >
@@ -500,7 +486,6 @@ If you had the following config:
500
486
$container->setDefinition('my_filter', ...);
501
487
$container->setDefinition('another_filter', ...);
502
488
$container->setDefinition('mail_manager', new Definition(
503
- '%mail_manager.class%'
504
489
))->setAbstract(
505
490
true
506
491
)->addMethodCall('setFilter', array(
@@ -518,5 +503,5 @@ In this example, the ``setFilter`` of the ``newsletter_manager`` service
518
503
will be called twice, resulting in the ``$filters `` array containing both
519
504
``my_filter `` and ``another_filter `` objects. This is great if you just want
520
505
to add additional filters to the subclasses. If you want to replace the filters
521
- passed to the subclass, removing the parent setting from the config will
506
+ passed to the subclass, removing the parent setting from the config will
522
507
prevent the base class from calling ``setFilter ``.
0 commit comments