Skip to content

Commit 43543bb

Browse files
committed
minor #4748 Re-reading private service section (weaverryan)
This PR was merged into the 2.3 branch. Discussion ---------- Re-reading private service section | Q | A | --- | --- | Doc fix? | yes | New docs? | kind of | Applies to | all | Fixed tickets | n/a Hi guys! This follows #4656. I merged that, but then realized that if you read the wider section, the top was still talking about private services as if their benefit was to *not* allow fetching directly (whereas the true emphasis now is on performance, and how `get()` may or may not work. Thanks! Commits ------- 8f5e210 Re-wording based on Wouter's recommendation 0f86a86 [#4656] Re-reading private service section
2 parents d9935a3 + 8f5e210 commit 43543bb

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

components/dependency_injection/advanced.rst

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,19 @@ Marking Services as public / private
1010
When defining services, you'll usually want to be able to access these definitions
1111
within your application code. These services are called ``public``. For example,
1212
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::
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+
7169
However, if a service has been marked as private, you can still alias it (see
7270
below) to access this service (via the alias).
7371

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy