对象的序列化和致命错误
谁能解释一下,为什么在这种情况下的会话中我们得到具有 2 属性的对象?
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:在这里我希望得到:
1. 致命错误(通过)
2. 会话中的对象(在会话文件中),具有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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是致命错误对于引擎来说是致命。之后,引擎无法再调用任何函数。
因此,在 php_var_serialize_intern 中,调用
__sleep
失败。正如您所看到的,如果 __sleep 抛出异常,或者根本没有任何 __sleep 回调,则不需要致命错误,该行为将相似。具体来说,该行为是检索变量的所有实例属性并序列化生成的哈希表,就好像它属于数组一样。
我认为这是一种有效的方法,但也许您认为如果对 __sleep 的调用失败,序列化就应该失败。您可以尝试提交功能请求。
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.