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

Questions regarding session

Hi, I'm trying to figure out some aspects regarding that session.

First of all is there a difference between:

(A) new SessionAdapter(); and (B) new Session();

I am asking as the docs suggest that it should be (B) but webtools generate (A) and it seems to work.

The other thing is wether I should use:

(C) $di->set( or (D) $di->setShared(

I ask because in the docs: https://docs.phalcon.io/en/latest/reference/session.html#isolating-session-data-between-applications in isolating example it is (C) without an argument "true". However in all other examples session is always used with (D)

I would be thankful for any clarification.

Regards, Maciek

SessionAdapter is just alias for proper session adapter. No difference here. About set or setShared. Set + true = setShared. The result is the same - singelton service, like you will get the same object accessing di each time. With false and set you will get new object each time.

Thank you for the answer.

In this link new object will be created each time: https://docs.phalcon.io/en/latest/reference/session.html#isolating-session-data-between-applications is it done on purpose? If so can you maybe explain why?

Is there a link explaining when it is better to use set and when setShared? I couldn't find it in documentation anywhere.



145.0k
Accepted
answer

It just depends on your logic. If you want have same object all the time - then use setShared, if you want to have new object each time you resolve it from di - use set. Most of time singleton will be enough for most of apps. In some apps there will be no difference between them because you will anyway resolve it only in one place etc. If in your application you are gonna access service in two seperated places with get method then it's matter. If not - don't care about this. Session or cookies can be example where you could possibly user setShared or getShared.

Also you can use getShared method even if service is registered as not shared.

Thank you.