I have a question regarding symfony2 lazy services. When we should use lazy services, and when we should avoid them? Is there any overhead if we use lazy services?
From the documentation:
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.
Yes, there is some overhead. But it is minimal. You should avoid using lazy services when you don't need them. (Easy as that).
Example:
If your service A has 3 methods and depends on B and C. If you know that B is used in all 3 methods and C i only used in one method then you may consider declaring C as lazy. You should declare it has lazy if C is a heavy service. In this example there will not be any benefit of declaring B as lazy... so don't... =)
templating
engine. –
Flatiron © 2022 - 2024 — McMap. All rights reserved.