I’m build some common class objects that will be tucked in the $_SESSION object routinely. I learned that while adding an object to a session sometimes it can be added without serialize but it is unreliable so i serialize first and it works everytime.
I find the following to be incredibly tedius/repetive and clunky… its like i have to check out the object from the session do some work and then remember to check it in the right way. Is there a better/more creative way that is also efficient and re-useable. help php experts
// Painful Code
$tmpObj = new clsSession; // Class setup for intellisense to work.
$tmpObj = unserialize($_SESSION['objSession']); // I assign so i can get intellisense as well.
$tmpObj //... do some work make some changes some other logic could be lengthy...
// and then i have to remember to do the following
$_SESSION['objSession'] = serialize($tmpObj);
unset $tmpObj;
/* or better */
$tmpObj = $_SESSION['objSession'];
$tmpObj = unserialize($tmpObj);
$tmpObj //... do some work make some changes some other logic
//and then i have to remember to do the following
$_SESSION['objSession'] = serialize($tmpObj);
unset $tmpObj;
Tim, Thank you for your input. Years ago I experienced stability problems with objects and sessions; serialized and non-serialized. Missing class definitions during session deserialization are common. If a class definitions undefined, PHP creates incomplete objects of the class __PHP_Incomplete_Class. This degenerated object misses every method. To avoid this error, every class definition must be included before starting the session. So if you implement your auto-loader prior to starting a session you will not have any issues. This solution has worked well for the last 6+ years.