Warning: You are browsing the documentation for version 3.1 which is not maintained anymore.
If some of your projects are still using this version, consider upgrading.
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
in your service definition:
1 2 3 4 5 6
# app/config/services.yml services: app.some_not_shared_service: class: ... shared: false # ...
1 2 3 4
<!-- app/config/services.xml --> <services> <service id="app.some_not_shared_service" class="..." shared="false" /> </services>
1 2 3 4 5 6 7
// app/config/services.php use Symfony\Component\DependencyInjection\Definition; $definition = new Definition('...'); $definition->setShared(false); $container->setDefinition('app.some_not_shared_service', $definition);
Now, whenever you call
inject this service, you'll receive a new instance.