NYCPHP Meetup

NYPHP.org

[nycphp-talk] Sessions and Classes

Dan Cech dcech at phpwerx.net
Mon Oct 4 09:50:53 EDT 2004


Jeff Knight wrote:
> You can find a useful discussion of this issue here:
> http://lists.nyphp.org/pipermail/talk/2004-September/012133.html
> 
> I would reccomend in particular Dan Cech's explanation(s) as to why
> you don't need to serialize objects in a session.

Thanks for the 'mad pr0pz' Jeff ;)

The biggest thing to remember when storing objects in the $_SESSION
array is that you must have the class definitions loaded before you try
to open the session.

In your main script you would want to have something like this:

<?php

include('myclass1.inc.php');
include('myclass2.inc.php');

session_start();

if (!isset($_SESSION['myobject1'])) {
   $_SESSION['myobject1'] = new myclass1();
}

if (!isset($_SESSION['myobject2'])) {
   $_SESSION['myobject2'] = new myclass2();
}

$_SESSION['myobject1']->foo();


This is potentially a topic for a phundamental, which could also cover
the use of the __autoload functionality in php5.

Dan




More information about the talk mailing list