-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #5922 Added minimal cookbook article about the shared flag (W…
…outerJ) This PR was merged into the 2.8 branch. Discussion ---------- Added minimal cookbook article about the shared flag | Q | A | --- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#14984) | Applies to | 2.8+ | Fixed tickets | #5437 Commits ------- 943ee0c Added minimal cookbook article about shared
- Loading branch information
Showing
4 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ Service Container | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
shared | ||
scopes | ||
compiler_passes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
.. index:: | ||
single: Service Container; Shared Services | ||
|
||
How to Define Not Shared Services | ||
================================= | ||
|
||
.. versionadded:: 2.8 | ||
The ``shared`` setting was introduced in Symfony 2.8. Prior to Symfony 2.8, | ||
you had to use the ``prototype`` scope. | ||
|
||
In the service container, all services are shared by default. This means that | ||
each time you retrieve the service, you'll get the *same* instance. This is | ||
often the behaviour you want, but in some cases, you might want to always get a | ||
*new* instance. | ||
|
||
In order to always get a new instance, set the ``shared`` setting to ``false`` | ||
in your service definition: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
services: | ||
app.some_not_shared_service: | ||
class: ... | ||
shared: false | ||
# ... | ||
.. code-block:: xml | ||
<services> | ||
<service id="app.some_not_shared_service" class="..." shared="false" /> | ||
</services> | ||
.. code-block:: php | ||
$definition = new Definition('...'); | ||
$definition->setShared(false); | ||
$container->setDefinition('app.some_not_shared_service', $definition); | ||
Now, whenever you call ``$container->get('app.some_not_shared_service')`` or | ||
inject this service, you'll recieve a new instance. |