limes Posted August 13, 2007 Share Posted August 13, 2007 I use PHP 5.1.4 and need the session to save objects in it. An abstract example: class House{ public $objRoom = array(); public $objType; public __construct(){ $this->objRoom[] = new Room(); $this->objType = new HType(); } public addRoom(){ $this->objRoom[] = new Room(); } } class Room{ public $name; public $size; public __construct(){ } public setName($name){ $this->name = $name; } } class HType{ ... } Firstly, after successfully logon I register my object like that: ini_set("session.use_trans_sid",true); session_start(); autoloader for classes... $_SESSION['myHouse'] = new House(); And then, I open a new window with javascript and close the previous. I open a file looks like as follows <?php ini_set("session.use_trans_sid",true); session_start(); here comes the autoloader for the classes... if(isset($_GET['act']) AND $_GET['act']=="addRoom"){ $_SESSION['myHouse']->addRoom(); } if(isset($_GET['name']) AND $_isset($_GET['act']) AND $_GET['act']=="setRoomName"){ $_SESSION['myHouse']->objRoom[$objRef]->setName($_GET['name']); } ?> <html> .... some code </html> While I add and delete rooms or change their names I loose the content of the session. All my objects are disappeared and the session file is empty! I activated errror_reporting(EALL) at the beginning of each page. I get only an error message when the object myHouse is disappeared. What wrong do I make? Quote Link to comment https://forums.phpfreaks.com/topic/64754-session-problems-with-objects/ Share on other sites More sharing options...
lemmin Posted August 13, 2007 Share Posted August 13, 2007 You need to serialize the object in order to save it externally. http://us.php.net/manual/en/function.serialize.php Quote Link to comment https://forums.phpfreaks.com/topic/64754-session-problems-with-objects/#findComment-322979 Share on other sites More sharing options...
limes Posted August 19, 2007 Author Share Posted August 19, 2007 Thanks I tried to serialize the object and to save it in the session. But on this way I have the same problem. ... $myH = new House(); ... $_SESSION['myHouse'] = serialize($myH); ... ... $myH = unserialize($_SESSION['myHouse']); ... Quote Link to comment https://forums.phpfreaks.com/topic/64754-session-problems-with-objects/#findComment-328343 Share on other sites More sharing options...
trq Posted August 19, 2007 Share Posted August 19, 2007 Sessions are automatically serialized. No need to worry about it. What you must do however is save the object locally before using it. eg; <?php session_start(); $house = $_SESSION['myHouse']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64754-session-problems-with-objects/#findComment-328348 Share on other sites More sharing options...
limes Posted August 26, 2007 Author Share Posted August 26, 2007 Mmh, also with this suggestion I couldn't eliminate the curious effect. I've spent hours to find out the reason. Quote Link to comment https://forums.phpfreaks.com/topic/64754-session-problems-with-objects/#findComment-334710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.