1
1
.. index ::
2
2
single: Service Container; Shared Services
3
3
4
- How to Define Not Shared Services
4
+ How to Define Non Shared Services
5
5
=================================
6
6
7
7
.. versionadded :: 2.8
@@ -10,7 +10,7 @@ How to Define Not Shared Services
10
10
11
11
In the service container, all services are shared by default. This means that
12
12
each 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
14
14
*new * instance.
15
15
16
16
In order to always get a new instance, set the ``shared `` setting to ``false ``
@@ -20,6 +20,7 @@ in your service definition:
20
20
21
21
.. code-block :: yaml
22
22
23
+ # app/config/services.yml
23
24
services :
24
25
app.some_not_shared_service :
25
26
class : ...
@@ -28,16 +29,20 @@ in your service definition:
28
29
29
30
.. code-block :: xml
30
31
32
+ <!-- app/config/services.xml -->
31
33
<services >
32
34
<service id =" app.some_not_shared_service" class =" ..." shared =" false" />
33
35
</services >
34
36
35
37
.. code-block :: php
36
38
39
+ use Symfony\Component\DependencyInjection\Definition;
40
+
41
+ // app/config/services.php
37
42
$definition = new Definition('...');
38
43
$definition->setShared(false);
39
44
40
45
$container->setDefinition('app.some_not_shared_service', $definition);
41
46
42
47
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