Should I remove unused services from a service container in Symfony 2?

I do not know if this has advantages: should I remove unused services from the service container?

For example, in the configuration of my package, I can disable the use of the lang option provider:

 my_bundle: providers: lang: enabled: false 

When the lang provider is enabled, the provider tag must be added. This service is defined in the services.xml file and is loaded through the extension:

 <service id="my_bundle.provider.lang" class="My\Provider\LangOptionsProvider"> 

So, it is a good idea to remove this definition and why?

 public function load(array $configs, ContainerBuilder $container) { // ... $loader->load('services.xml'); // Loads "my_bundle.provider.lang" if($config['providers']['lang']['enabled']) { // lang provider is enabled, add the tag $container->getDefinition('my_bundle.provider.lang')->addTag('provider'); } else { // Is this really needed? // Remove lang provider definition (or just the tag?!) $container->removeDefinition('my_bundle.provider.lang'); } } 
+4
source share
1 answer

The overhead is minimal, as the instance is created when the service is requested, so it does not matter if you delete it or not.

When you request the my_mailer service from the container, the container creates an object and returns it. This is another important advantage of using a container service. Namely, the service is never built until it is needed. If you define a service and never use it in a request, the service is never created. This saves memory and increases the speed of your application. It also means that there is very little or no performance for defining multiple services. Services that are never used are never created.

http://symfony.com/doc/current/book/service_container.html

+4
source

All Articles