11.. index ::
22 single: Service Container; Shared Services
33
4- How to Define Not Shared Services
4+ How to Define Non Shared Services
55=================================
66
77.. versionadded :: 2.8
@@ -10,7 +10,7 @@ How to Define Not Shared Services
1010
1111In the service container, all services are shared by default. This means that
1212each time you retrieve the service, you'll get the *same * instance. This is
13- often the behaviour you want, but in some cases, you might want to always get a
13+ often the behavior you want, but in some cases, you might want to always get a
1414*new * instance.
1515
1616In order to always get a new instance, set the ``shared `` setting to ``false ``
@@ -20,6 +20,7 @@ in your service definition:
2020
2121 .. code-block :: yaml
2222
23+ # app/config/services.yml
2324 services :
2425 app.some_not_shared_service :
2526 class : ...
@@ -28,16 +29,20 @@ in your service definition:
2829
2930 .. code-block :: xml
3031
32+ <!-- app/config/services.xml -->
3133 <services >
3234 <service id =" app.some_not_shared_service" class =" ..." shared =" false" />
3335 </services >
3436
3537 .. code-block :: php
3638
39+ use Symfony\Component\DependencyInjection\Definition;
40+
41+ // app/config/services.php
3742 $definition = new Definition('...');
3843 $definition->setShared(false);
3944
4045 $container->setDefinition('app.some_not_shared_service', $definition);
4146
4247 Now, whenever you call ``$container->get('app.some_not_shared_service') `` or
43- inject this service, you'll recieve a new instance.
48+ inject this service, you'll receive a new instance.
0 commit comments