views:

92

answers:

1

Can anyone explain me, why in the session in this case we get the object with 2 properties?

set_error_handler('my_error_handler');

session_start();

$obj = new myclass();

$_SESSION['obj'] = $obj;

$a->b();

class myclass
{
    private $a = 1;
    private $b = 2;

    public function __sleep()
    {
        return array('a');
    }
}

function my_error_handler($code, $error, $file = NULL, $line = NULL)
{
    throw new ErrorException($error, $code, 0, $file, $line);
}

UPD: here i expect to get:
1. fatal error (passed)
2. object in session (in session file) with 1 property (failed)

A: 

The reason for this is that a fatal error is, well, fatal to the engine. After it, the engine cannot call anymore functions.

Hence, in php_var_serialize_intern the call to __sleep fails. As you can see, you don't need a fatal error, if __sleep had thrown an exception, or if there wasn't any __sleep callback at all, the behavior would be similar.

In particular, the behavior is to retrieve all the instance properties of the variable and to serialize the resulting hash table as if it belonged to an array.

I think this is a valid approach, but perhaps you think that if the call to __sleep fails, the serialization should just fail. You can try to submit a feature request.

Artefacto
i've filled bug http://bugs.php.net/bug.php?id=52604. "I think this is a valid approach" -- it breaks my data, and it is definitely not what i'm expecting. i expect my data to be safe ;-)
zerkms
so, since it is bogus/bug and it is following the current implementation - there is nothing more to discuss. if no answers will be added within next day - i will accept this answer (i will accept it in any case, but if anyone has thoughts about what should happen in my sample code - i would like to discuss)
zerkms