SymfonyWorld 2022 Summer Edition
100% Online
+20 talks and workshops
Starts in 18 days

How to Define Non Shared Services

Edit this page

Warning: You are browsing the documentation for Symfony 2.8, which is no longer maintained.

Read the updated version of this page for Symfony 6.1 (the current stable version).

How to Define Non Shared Services


The shared option 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 behavior 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:

  • YAML
  • XML
  • PHP
# app/config/services.yml
        class: ...
        shared: false
        # ...

Now, whenever you call $container->get('app.some_not_shared_service') or inject this service, you'll receive a new instance.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.