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.

What is the difference between setShared and set?

Hello.

$di->setShared('session', function () { $session = new SessionAdapter(); $session->start();

return $session;

});

$di->set('session', function () { $session = new SessionAdapter(); $session->start();

return $session;

});

All the codes are working. Is there any difference?

Thanks.



1.6k
Accepted
answer
edited Nov '16

setShared always return same instantion of SessionAdapter

Shared services works as singletons.

Here you have explanation: https://docs.phalconphp.com/pl/latest/reference/di.html#shared-services