Skip to content

Commit 0757f65

Browse files
Removing class key/attribute from parents services in examples
1 parent 9dfb144 commit 0757f65

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

components/dependency_injection/parentservices.rst

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -184,34 +184,31 @@ a parent for a service.
184184
# ...
185185
newsletter_manager.class: NewsletterManager
186186
greeting_card_manager.class: GreetingCardManager
187-
mail_manager.class: MailManager
188187
services:
189188
my_mailer:
190189
# ...
191190
my_email_formatter:
192191
# ...
193192
mail_manager:
194-
class: "%mail_manager.class%"
195193
abstract: true
196194
calls:
197195
- [ setMailer, [ @my_mailer ] ]
198196
- [ setEmailFormatter, [ @my_email_formatter] ]
199-
197+
200198
newsletter_manager:
201199
class: "%newsletter_manager.class%"
202200
parent: mail_manager
203-
201+
204202
greeting_card_manager:
205203
class: "%greeting_card_manager.class%"
206204
parent: mail_manager
207-
205+
208206
.. code-block:: xml
209207
210208
<parameters>
211209
<!-- ... -->
212210
<parameter key="newsletter_manager.class">NewsletterManager</parameter>
213211
<parameter key="greeting_card_manager.class">GreetingCardManager</parameter>
214-
<parameter key="mail_manager.class">MailManager</parameter>
215212
</parameters>
216213
217214
<services>
@@ -221,7 +218,7 @@ a parent for a service.
221218
<service id="my_email_formatter" ...>
222219
<!-- ... -->
223220
</service>
224-
<service id="mail_manager" class="%mail_manager.class%" abstract="true">
221+
<service id="mail_manager" abstract="true">
225222
<call method="setMailer">
226223
<argument type="service" id="my_mailer" />
227224
</call>
@@ -242,12 +239,10 @@ a parent for a service.
242239
// ...
243240
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
244241
$container->setParameter('greeting_card_manager.class', 'GreetingCardManager');
245-
$container->setParameter('mail_manager.class', 'MailManager');
246242
247243
$container->setDefinition('my_mailer', ...);
248244
$container->setDefinition('my_email_formatter', ...);
249245
$container->setDefinition('mail_manager', new Definition(
250-
'%mail_manager.class%'
251246
))->setAbstract(
252247
true
253248
)->addMethodCall('setMailer', array(
@@ -279,16 +274,15 @@ when the child services are instantiated.
279274
defined on the ``mail_manager`` service will not be executed when the
280275
child services are instantiated.
281276

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.
287281

288282
.. note::
289283

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`
292286
for more details.
293287

294288
Overriding Parent Dependencies
@@ -308,7 +302,6 @@ to the ``NewsletterManager`` class, the config would look like this:
308302
# ...
309303
newsletter_manager.class: NewsletterManager
310304
greeting_card_manager.class: GreetingCardManager
311-
mail_manager.class: MailManager
312305
services:
313306
my_mailer:
314307
# ...
@@ -317,29 +310,27 @@ to the ``NewsletterManager`` class, the config would look like this:
317310
my_email_formatter:
318311
# ...
319312
mail_manager:
320-
class: "%mail_manager.class%"
321313
abstract: true
322314
calls:
323315
- [ setMailer, [ @my_mailer ] ]
324316
- [ setEmailFormatter, [ @my_email_formatter] ]
325-
317+
326318
newsletter_manager:
327319
class: "%newsletter_manager.class%"
328320
parent: mail_manager
329321
calls:
330322
- [ setMailer, [ @my_alternative_mailer ] ]
331-
323+
332324
greeting_card_manager:
333325
class: "%greeting_card_manager.class%"
334326
parent: mail_manager
335-
327+
336328
.. code-block:: xml
337329
338330
<parameters>
339331
<!-- ... -->
340332
<parameter key="newsletter_manager.class">NewsletterManager</parameter>
341333
<parameter key="greeting_card_manager.class">GreetingCardManager</parameter>
342-
<parameter key="mail_manager.class">MailManager</parameter>
343334
</parameters>
344335
345336
<services>
@@ -352,7 +343,7 @@ to the ``NewsletterManager`` class, the config would look like this:
352343
<service id="my_email_formatter" ...>
353344
<!-- ... -->
354345
</service>
355-
<service id="mail_manager" class="%mail_manager.class%" abstract="true">
346+
<service id="mail_manager" abstract="true">
356347
<call method="setMailer">
357348
<argument type="service" id="my_mailer" />
358349
</call>
@@ -377,13 +368,11 @@ to the ``NewsletterManager`` class, the config would look like this:
377368
// ...
378369
$container->setParameter('newsletter_manager.class', 'NewsletterManager');
379370
$container->setParameter('greeting_card_manager.class', 'GreetingCardManager');
380-
$container->setParameter('mail_manager.class', 'MailManager');
381371
382372
$container->setDefinition('my_mailer', ...);
383373
$container->setDefinition('my_alternative_mailer', ...);
384374
$container->setDefinition('my_email_formatter', ...);
385375
$container->setDefinition('mail_manager', new Definition(
386-
'%mail_manager.class%'
387376
))->setAbstract(
388377
true
389378
)->addMethodCall('setMailer', array(
@@ -442,30 +431,27 @@ If you had the following config:
442431
parameters:
443432
# ...
444433
newsletter_manager.class: NewsletterManager
445-
mail_manager.class: MailManager
446434
services:
447435
my_filter:
448436
# ...
449437
another_filter:
450438
# ...
451439
mail_manager:
452-
class: "%mail_manager.class%"
453440
abstract: true
454441
calls:
455442
- [ setFilter, [ @my_filter ] ]
456-
443+
457444
newsletter_manager:
458445
class: "%newsletter_manager.class%"
459446
parent: mail_manager
460447
calls:
461448
- [ setFilter, [ @another_filter ] ]
462-
449+
463450
.. code-block:: xml
464451
465452
<parameters>
466453
<!-- ... -->
467454
<parameter key="newsletter_manager.class">NewsletterManager</parameter>
468-
<parameter key="mail_manager.class">MailManager</parameter>
469455
</parameters>
470456
471457
<services>
@@ -475,7 +461,7 @@ If you had the following config:
475461
<service id="another_filter" ...>
476462
<!-- ... -->
477463
</service>
478-
<service id="mail_manager" class="%mail_manager.class%" abstract="true">
464+
<service id="mail_manager" abstract="true">
479465
<call method="setFilter">
480466
<argument type="service" id="my_filter" />
481467
</call>
@@ -500,7 +486,6 @@ If you had the following config:
500486
$container->setDefinition('my_filter', ...);
501487
$container->setDefinition('another_filter', ...);
502488
$container->setDefinition('mail_manager', new Definition(
503-
'%mail_manager.class%'
504489
))->setAbstract(
505490
true
506491
)->addMethodCall('setFilter', array(
@@ -518,5 +503,5 @@ In this example, the ``setFilter`` of the ``newsletter_manager`` service
518503
will be called twice, resulting in the ``$filters`` array containing both
519504
``my_filter`` and ``another_filter`` objects. This is great if you just want
520505
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
522507
prevent the base class from calling ``setFilter``.

0 commit comments

Comments
 (0)