Lazy Services

Same name and namespace in other branches
  1. 10 core/core.api.php \lazy_services
  2. 11.x core/core.api.php \lazy_services

Lazy services overview

A service can be declared as lazy in order to improve performance. Classes that inject a lazy service receive a proxy class instead, and when a method on the lazy service is called, the proxy class gets the service from the container and forwards the method call. This means that the lazy service is only instantiated when it is needed.

This is useful because some classes may inject a service which is expensive to instantiate (because it has multiple dependencies of its own), but is only used in exceptional cases. This would make the class dependent on the expensive service and all of the expensive service's dependencies.

Making the expensive service lazy means that the class is only dependent on the proxy service, and not on all the dependencies of the lazy service.

To define a service as lazy, add

lazy:
true;

to the service definition, and use the

core / scripts / generate - proxy . sh;

script to generate the proxy class.

See also

core/scripts/generate-proxy.sh

File

core/core.api.php, line 2542

Sub-Topics

Title Sort descending File name Summary
Events core/core.api.php Overview of event dispatch and subscribing
Sessions core/core.api.php Store and retrieve data associated with a user's browsing session.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.