Thomas Munz wrote:

I think, its not possible to init an Objeect on a session.

Yes, it can.

The problem was with serialization. With session.auto_start set to 1 on php.ini, seems like session's objects are unserialized before loading the script, so the class is not loaded when the session unserialized the object, and therefore it fails to unserialize it as an instance of its class.

Setting session.auto_start to 0 on php.ini you should take care of resume sessions calling session_start() each time, but you can put that call after class definition, so the object will be unserialized after, so the problem is solved.

This code works fine:


<?php

class SessionTestC {
   protected $value;

   function __construct ( $val ) {
       $this->value = $val;
   }

   function GetValue () {
       return $this->value;
   }
}

if ( isset ($_GET['close_session'])) {
   unset ($_SESSION);
   session_start ();
   session_destroy ();
}

session_start ();

if ( isset ($_SESSION['TestObj'])) {
echo 'TestObj is an instance of '.get_class($_SESSION['TestObj']).'<br>';
echo '<pre>';
print_r ($SESSION['TestObj']);
echo '</pre>';


echo "<a href='".basename($_SERVER['PHP_SELF'])."?close_session=1'>Close session.</a><br>";
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>";
}


?>


BTW, I do not know if the code I've used to destroy the session is the best, but this is a secondary issue...


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to