Re: [PHP] object persistence within a session



On Fri, Nov 7, 2008 at 8:33 AM, Stan <stanleytberry@xxxxxxxxx> wrote:
If this is the wrong forum, please point me at the correct forum.

I am new to PHP but have 40 years experience programming.

My initial effort includes a class definition which needs to persist for the duration of a WWW session. The code (this snippet is the beginning of Default.php)

<?PHP
session_start();
require_once 'CSSFrames_Includes/Classes.inc';
if (!isset($_SESSION["navigator"]))
{
$_SESSION["navigator"] = new CSSFrames_Navigator;
}
else
{
parse_str($_SERVER['QUERY_STRING'], $queryString);
foreach ($queryString as $key => $value)
{
switch($key)
{
case "ID":
$_SESSION["navigator"]->set_page_ID($value);
break;
default:
break;
}
}
}
initially works ... $_session["navigator"] does exist and methods do function correctly. When, however, I click a link and cause the script (Default.php) to be reentered, I get the following

Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;MyClass_defined&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /MyWebSite.com/Default.php on line 16

which I understand to mean that my class definition (file?) has not been loaded. What I do not understand is why has not the class definition been loaded? Did storing the object reference not also store the class definition? What must I do to retain the class definition as part of the session data?

Thanks,
Stan

In the example you posted, you should be OK if you just switch the
first two lines. You have to declare the class before you start the
session.

http://www.php.net/manual/en/language.oop.serialization.php

Andrew
.



Relevant Pages