.. index:: single: Dependency Injection; Lazy Services
.. versionadded:: 2.3 Lazy services were added in Symfony 2.3.
In some cases, you may want to inject a service that is a bit heavy to instantiate,
but is not always used inside your object. For example, imagine you have
a NewsletterManager
and you inject a mailer
service into it. Only
a few methods on your NewsletterManager
actually use the mailer
,
but even when you don't need it, a mailer
service is always instantiated
in order to construct your NewsletterManager
.
Configuring lazy services is one answer to this. With a lazy service, a "proxy"
of the mailer
service is actually injected. It looks and acts just like
the mailer
, except that the mailer
isn't actually instantiated until
you interact with the proxy in some way.
In order to use the lazy service instantiation, you will first need to install the ProxyManager bridge:
$ php composer.phar require symfony/proxy-manager-bridge:2.3.*
Note
If you're using the full-stack framework, this package is not included
and needs to be added to composer.json
and installed (which is what
the above command does).
You can mark the service as lazy
by manipulating its definition:
.. configuration-block:: .. code-block:: yaml services: foo: class: Acme\Foo lazy: true .. code-block:: xml <service id="foo" class="Acme\Foo" lazy="true" /> .. code-block:: php $definition = new Definition('Acme\Foo'); $definition->setLazy(true); $container->setDefinition('foo', $definition);
You can then require the service from the container:
$service = $container->get('foo');
At this point the retrieved $service
should be a virtual proxy with
the same signature of the class representing the service. You can also inject
the service just like normal into other services. The object that's actually
injected will be the proxy.
Note
If you don't install the ProxyManager bridge, the container will just
skip over the lazy
flag and simply instantiate the service as it would
normally do.
The proxy gets initialized and the actual service is instantiated as soon as you interact in any way with this object.
You can read more about how proxies are instantiated, generated and initialized in the documentation of ProxyManager.