-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added minimal cookbook article about the shared flag #5922
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ Service Container | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
shared | ||
scopes | ||
compiler_passes |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. behavior |
||
*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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
app.some_not_shared_service: | ||
class: ... | ||
shared: false | ||
# ... | ||
.. code-block:: xml | ||
<services> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
<service id="app.some_not_shared_service" class="..." shared="false" /> | ||
</services> | ||
.. code-block:: php | ||
$definition = new Definition('...'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and missing |
||
$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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: recieve -> receive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"not shared", "non shared", "unshared", ...? (in other words: I'm not sure how to call this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO "non shared"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for "non shared"