We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



3.5k
Accepted
answer
edited Nov '16

setShared always return same instantion of SessionAdapter

Shared services works as singletons.

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