Skip to content

Commit

Permalink
minor #4656 Tried to clarify private services (WouterJ)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

Tried to clarify private services

| Q   | A
| --- | ---
| Doc fix? | yes
| New docs? | kind of
| Applies to | all
| Fixed tickets | #4524

It was a though one to describe, as I wanted to make it clear and short. I'm really happy if people can review this and provide other, better, alternatives!

Commits
-------

d89ad21 Tried to clarify private services
  • Loading branch information
weaverryan committed Jan 3, 2015
2 parents 127ebc1 + d89ad21 commit 9958c41
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions components/dependency_injection/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ argument for another service.

.. _inlined-private-services:

.. note::
Since a container is not able to detect if a service is retrieved from inside
the container or the outside, a private service may still be retrieved using
the ``get()`` method.

If you use a private service as an argument to only one other service,
this will result in an inlined instantiation (e.g. ``new PrivateFooBar()``)
inside this other service, making it publicly unavailable at runtime.
What makes private services special, is that they are converted from services
to inlined instantiation (e.g. ``new PrivateThing()``) when they are only
injected once, to increase the container performance. This means that you can
never be sure if a private service exists in the container.

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

$container->get('foo');

Expand Down

0 comments on commit 9958c41

Please sign in to comment.