RE: [PHP] How to preserve instance of class within sessions ???

From: Date: Fri, 09 May 2003 01:16:28 +0000
Subject: RE: [PHP] How to preserve instance of class within sessions ???
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-146954@lists.php.net to get a copy of this message
> First of all, read the manual pages on sessions thoroughly. <cut> > And yes, you _can_ make objects session persistent, PROVIDED that the > necessary class definitions are loaded/included/defined _before_ the > session gets reconstructed. <cut> > If however you would put session_start() before the class definition, or > have session.auto_start enabled in php.ini, this cannot work - > PHP needs to know the class definition when reconstructing an object from session data. > If this is missing it will resort to a default class where all data > properties are preserved, but of course no methods are available. Also look into the magic functions. __wakeup and __sleep http://www.php.net/manual/en/language.oop.magic-functions.php If you put these methods in your object PHP will clean up the object upon serialization and setup things you need upon unserializing. I usually have __sleep close off all of my connections, store any state data I want on the next page, and clean out any members I don't want to persist. Then I have __wakeup re-establish database connections and anything else I need re-established since the constructor won't be called. I hope this helps a little. To tell the truth, I only started using them because the manual said they were magic.

« previous php.general (#146954) next »