I’m writing a small non-commercial web app, and I’m wondering how to pass data from one piece of the code to others. Specifically, I want to output various messages to the user depending on the circumstances (“you’re logged in”, “logged out”, “status updated”, etc). (At this point, I don’t see the case where I would need to keep several messages in memory, but it’s not really relevant.) So I could structure my code with a $_SESSION['message'] variable, or I could systematically pass an instance of a class Message, or even a string variable, from one php file to the next.
I’m not sure if there are compelling reasons to use one approach rather than the other.
EDIT: The “pieces of code” I make reference to above are MVC Controllers that decide which function to execute and what to display. They then redirect to a front controller, so the instance changes. The front controller then loads the views.
The question makes not much sense to me.
In case you are talking of different PHP instances (for example, if you made a redirect after succesful login), you have a little choice but sessions.
In case you are talking of passing messages between code pieces within the same PHP script instance, there is no use for the sessions at all.