Let’s say that in my app I have an object instance created on page 1. The user then goes to some other part of app and I want the instance to remain. How can I ‘save’ the instance? Sessions?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, use a session.
Call session_start() at the beginning of your page, then store your object with something like
$_SESSION['myobject']=$myobject;The later page can access
$_SESSION['myobject']after it too calls session_start()You need to make sure that any page which uses that session has the class for the object defined or is capable of auto-loading it.
Your class can also define the magic methods __sleep and __wakeup which allow you to clean up any member variables you don’t want serializing (like resources, such as db handles). During __wakeup you can restore these.