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

 

2 Responses to PHP – Save an object as a session variable?

  1. aaron says:

    aye. seriously. that’s a really awesome tip, i never would have thought of that.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>