Skip to content

Commit d89ad21

Browse files
committed
Tried to clarify private services
1 parent a51c623 commit d89ad21

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

components/dependency_injection/advanced.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ argument for another service.
2020

2121
.. _inlined-private-services:
2222

23-
.. note::
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.
2426

25-
If you use a private service as an argument to only one other service,
26-
this will result in an inlined instantiation (e.g. ``new PrivateFooBar()``)
27-
inside this other service, making it publicly unavailable at runtime.
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.
2831

2932
Simply said: A service will be private when you do not want to access it
3033
directly from your code.
@@ -60,7 +63,8 @@ Here is an example:
6063
$definition->setPublic(false);
6164
$container->setDefinition('foo', $definition);
6265
63-
Now that the service is private, you *cannot* call::
66+
Now that the service is private, you *should not* call (should not means, this
67+
*might* fail, see the explaination above)::
6468

6569
$container->get('foo');
6670

0 commit comments

Comments
 (0)