PHP – Save an object as a session variable?
Saving the state of an object to your session can be very useful, and can be accomplished by using a combination of the serialize/unserialize functions.
class.php
<?php
class warebot
{ //class implementation
function warebot(){}
}
?>
class.php has our simple class definition/implementation.
page1.php
<?php
session_start();
require_once('class.php');
$object = new warebot();
$_SESSION['object'] = serialize($object);
?>
page1.php is where we will create the object, serialize it and save the serialized result to our session
page2.php
<?session_start();
require_once('class.php');
$object = unserialize($_SESSION['object']);
?>
page2.php is where we will unserialize the previously serialized object (saved in our session)
Voila! you know have complete access to your original object.
-WAREBOT






aye. seriously. that’s a really awesome tip, i never would have thought of that.
Awesome Aaron!
Glad you enjoyed it