Skip to content

LazyService

Mauro Gadaleta edited this page Mar 6, 2017 · 1 revision

Lazy Service

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. Configuring lazy services is one answer to this. With a lazy service the container builder will instantiate the service only when we need it (during the container get call method)

YAML
services:
   app.mailer:
     class: ./App/Mailer
     lazy:  true
JSON
{
	"services": {
		"app.mailer": {
			"class": "./App/Mailer",
			"lazy": true
		}
	}
}
JS
let definition = new Definition(SomeClass)
definition.lazy = true

container.setDefinition('some_class', definition)