unserializing objects

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

class a {
    private $var;
    function __construct($var) { echo "constructor called\r\n"; $this-
Quoted text here. Click to load it
    function __set($var, $value) { $this->$var = $value; }
    function __get($var) { return $this->$var; }
    function __sleep() { echo "sleep called\r\n"; return array('var'
=> $this->var); }
    function __wakeup() { echo "wakeup called\r\n"; }

$a = new a('hello world!');
$a->var = 'test';
$b = serialize($a);

Shouldn't var_dump($a) and var_dump(unserialize($b)) yield the same
thing?  ie. this?:

object(a)#1 (1) {
  string(4) "test"

Because that's not what I'm getting.  What I'm getting is this:

object(a)#2 (2) {

Maybe this is a bug in PHP?  Maybe I just don't know what I'm doing?
Either way, if someone could provide some insight it'd be appreciated
- thanks!

Re: unserializing objects


Quoted text here. Click to load it

First you should fix the error reporting on your development machine.
Check your php.ini and make sure that these values are set correctly:

error_reporting = E_ALL|E_STRICT
display_errors = on

Then you will get an E_NOTICE error when you call serialize().

Next have a look at the manual to see what __sleep() is supposed to do
and what it should return.


Site Timeline