@@ -10,29 +10,19 @@ Marking Services as public / private
10
10
When defining services, you'll usually want to be able to access these definitions
11
11
within your application code. These services are called ``public ``. For example,
12
12
the ``doctrine `` service registered with the container when using the DoctrineBundle
13
- is a public service as you can access it via::
13
+ is a public service. This means that you can fetch it from the container
14
+ using the ``get() `` method::
14
15
15
16
$doctrine = $container->get('doctrine');
16
17
17
- However, there are use- cases when you don't want a service to be public. This
18
- is common when a service is only defined because it could be used as an
19
- argument for another service.
18
+ In some cases, a service * only * exists to be injected into another service
19
+ and is * not * intended to be fetched directly from the container as shown
20
+ above.
20
21
21
22
.. _inlined-private-services :
22
23
23
- Since a container is not able to detect if a service is retrieved from inside
24
- the container or the outside, a private service may still be retrieved using
25
- the ``get() `` method.
26
-
27
- What makes private services special, is that they are converted from services
28
- to inlined instantiation (e.g. ``new PrivateThing() ``) when they are only
29
- injected once, to increase the container performance. This means that you can
30
- never be sure if a private service exists in the container.
31
-
32
- Simply said: A service will be private when you do not want to access it
33
- directly from your code.
34
-
35
- Here is an example:
24
+ In these cases, to get a minor performance boost, you can set the service
25
+ to be *not * public (i.e. private):
36
26
37
27
.. configuration-block ::
38
28
@@ -63,11 +53,19 @@ Here is an example:
63
53
$definition->setPublic(false);
64
54
$container->setDefinition('foo', $definition);
65
55
66
- Now that the service is private, you *should not * call (should not means, this
67
- *might * fail, see the explaination above)::
56
+ What makes private services special is that, if they are only injected once,
57
+ they are converted from services to inlined instantiations (e.g. ``new PrivateThing() ``).
58
+ This increases the container's performance.
59
+
60
+ Now that the service is private, you *should not * fetch the service directly
61
+ from the container::
68
62
69
63
$container->get('foo');
70
64
65
+ This *may or may not work *, depending on if the service could be inlined.
66
+ Simply said: A service can be marked as private if you do not want to access
67
+ it directly from your code.
68
+
71
69
However, if a service has been marked as private, you can still alias it (see
72
70
below) to access this service (via the alias).
73
71
0 commit comments