Re: Objects and sessions

From: Date: Thu, 09 Dec 2004 13:42:47 +0000
Subject: Re: Objects and sessions
References: 1  Groups: php.general php.general 
Request: Send a blank email to php-general+get-203817@lists.php.net to get a copy of this message
Hi, your code my be this , you must use session_start() first: <?php session_start (); class SessionTestC { protected $value; function __construct ( $val ) {
       $this->value = $val;
} function GetValue () {
       return $this->value;
} } if ( isset ($_SESSION['TestObj'])) { echo 'Session Test is set to: '.$_SESSION['TestObj']->GetValue().'<br>'; } else { echo 'Session Test was not set.<br>'; $_SESSION['TestObj'] = new SessionTestC ( 'This is a test' ); echo "<a href='".basename($_SERVER['PHP_SELF'])."'>Click here.</a><br>"; } ?> Francisco M. Marzoa Alonso wrote:
Following code: <?php class SessionTestC { protected $value; function __construct ( $val ) {
       $this->value = $val;
} function GetValue () {
       return $this->value;
} } if ( isset ($_SESSION['TestObj'])) { echo 'Session Test is set to: '.$_SESSION['TestObj']->GetValue().'<br>'; } else { session_start (); echo 'Session Test was not set.<br>'; $_SESSION['TestObj'] = new SessionTestC ( 'This is a test' ); echo "<a href='".basename($_SERVER['PHP_SELF'])."'>Click here.</a><br>"; } ?> Gives me an error of incomplete object on second access, like this: *Fatal error*: main() [function.main <http://localhost/sfcms/Scripts/function.main>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "SessionTestC" 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 */home/fmmarzoa/Develop/Stradivarius/Scripts/SessionTest.php* on line *16 *But as it can be seen, the class is defined in the script SessionTest.php itself, so... ??????
-- -- ShenKong (shenkong@php.net) -- http://www.openphp.cn

« previous php.general (#203817) next »