Storing & Retrieving PHP Objects from Sessions

Status
Not open for further replies.

jason

New Member
As the title suggests, I'm having a problem getting this to work at all.

I believe I need to serialize when storing and unserialize when retrieving the object from $_SESSION

So I would have thought the following would have worked...
Code:
require_once('MyClass.ClassDef.php');

$obj = new MyClass();

$someval = $obj->getVal();

[B]$_SESSION['myobj'] = serialize($obj);
$objB = unserialize($_SESSION['myobj']);[/B]

$some_other_val = $objB->getVal();

//etc
I would have thought that objB == obj in the above

Yet when I pull the object out the other end of the session, it has lost the values, like it never serialized. I'm not getting any errors either. I also tried casting, like in other languages...

Code:
$objB = (MyClass)unserialize($_SESSION['myobj']);
This gives me errors, but the usual syntax errors you'd get from something misplaced. What am I missing?, do I need to change something in the environment?
 

fizzy

New Member
Hi Jason,

You probably sorted this out by now, but just in case, your code example works fine for me. You don't even need the serialize and unserialize. So, if you're still having issues, I assume that storing standard strings etc in the session is working for you? Do you have full error reporting enabled? On PHP 5 I find the highest level very useful: error_reporting = E_ALL | E_STRICT;
 
Status
Not open for further replies.
Top