@@ -10,29 +10,19 @@ Marking Services as public / private
1010When defining services, you'll usually want to be able to access these definitions
1111within your application code. These services are called ``public ``. For example,
1212the ``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::
1415
1516 $doctrine = $container->get('doctrine');
1617
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.
2021
2122.. _inlined-private-services :
2223
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):
3626
3727.. configuration-block ::
3828
@@ -63,11 +53,19 @@ Here is an example:
6353 $definition->setPublic(false);
6454 $container->setDefinition('foo', $definition);
6555
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::
6862
6963 $container->get('foo');
7064
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+
7169However, if a service has been marked as private, you can still alias it (see
7270below) to access this service (via the alias).
7371
0 commit comments