What is the best practice for passing objects in sessions in PHP?

When passing objects in sessions in PHP, it is best practice to serialize the object before storing it in the session and unserialize it when retrieving it. This ensures that the object is properly stored and retrieved without losing any data.

// Storing object in session
$object = new MyClass();
$_SESSION['myObject'] = serialize($object);

// Retrieving object from session
$object = unserialize($_SESSION['myObject']);