Let’s say we have index.php that has something like
$randomObj = new rndObject;
$_SESSION['object'] = $randomObj;
and securePage.php will have
$whatever = $_SESSION['object'];
vs
$randomObj = new rndObject;
$_SESSION['object'] = serialize($randomObj);
and securePage.php will have
$whatever = unserialize($_SESSION['object']);
I have tried both and it seems to have same results. So is there a benefit to serializing an object when passing it to a session?
When you pass an object to session, the object will be serialized by default. And you can implement the __sleep() method if you want to do some customize.