Serialization of objects and fatal error
-
28-09-2019 - |
Pergunta
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)
Solução
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.