Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

setShared service doesn't work

Hi, When i define a service like shared on dependency injector is correct call like:

$di->setShared("MYSERVICE", function(){ return new MYCLASS(); })

$this->MYSERVICE->MYMETHOD

always that i call the construct of my class "MYCLASS" is called again, Why?

thanks!



46.5k
Accepted
answer
edited May '16

Your custom class would only get instantiated on first resolve (i.e. first call from IoC).

Example:

$di->getShared('request'), //fresh instance

//........... code 

//........... code 

//........... code 

//........... code 

//........... more code ...


$di->getShared('request'), //shared instance