对象序列化__sleep

发布于 2024-10-19 19:52:39 字数 712 浏览 1 评论 0原文

php 手册指出:

它可以清理对象并且是 应该返回一个数组 该对象的所有变量的名称 应该序列化。

我的理解是,如果一个人上了一堂课。像这样:

<?php

class Foo {

    public $bar = 'bar';

    public $baz = 'baz';

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

}

$obj = new Foo();
$serialized = serialize($obj);
$unserialized = unserialize($serialized);

var_dump($unserialized);

?>

它只会序列化对象和属性 $bar?像这样:

object(Foo)[2]
  public 'bar' => string 'bar' (length=3)

但它返回:

object(Foo)[2]
  public 'bar' => string 'bar' (length=3)
  public 'baz' => string 'baz' (length=3)

我解释错了吗?还是我做错了还是什么?

the php manual states:

It can clean up the object and is
supposed to return an array with the
names of all variables of that object
that should be serialized.

i understand this as, if a had a class. Like this:

<?php

class Foo {

    public $bar = 'bar';

    public $baz = 'baz';

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

}

$obj = new Foo();
$serialized = serialize($obj);
$unserialized = unserialize($serialized);

var_dump($unserialized);

?>

it would only serialize the object and the property $bar? Like this:

object(Foo)[2]
  public 'bar' => string 'bar' (length=3)

but it returns:

object(Foo)[2]
  public 'bar' => string 'bar' (length=3)
  public 'baz' => string 'baz' (length=3)

Have i interpreted it wrong? Or am i doing it wrong or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-10-26 19:52:39

反序列化会创建对象的新实例,并且由于类的定义初始化了属性,因此您将获得它的默认值。试试这个:

class Foo {
    public $bar;
    public $baz;
    public function __sleep()
    {
        return array('bar');
    }
}

$obj = new Foo();
$obj->bar = 'bar';
$obj->baz = 'baz';
$serialized = serialize($obj);
$unserialized = unserialize($serialized);
var_dump($unserialized);

编辑:或者,您可以 vardump($serialized) 并查看其中没有 baz。

Unserializing creates a new instance of the object, and since your definition of the class initializes the attribute, you're getting a default value for it. Try this:

class Foo {
    public $bar;
    public $baz;
    public function __sleep()
    {
        return array('bar');
    }
}

$obj = new Foo();
$obj->bar = 'bar';
$obj->baz = 'baz';
$serialized = serialize($obj);
$unserialized = unserialize($serialized);
var_dump($unserialized);

Edit: Alternatively, you can vardump($serialized) and see that there is no baz in it.

和影子一齐双人舞 2024-10-26 19:52:39

您正在为 $baz 属性定义初始值“baz”,因此当您反序列化时,PHP 会使用该默认值重新创建 baz,尽管它不是序列化对象的一部分。
如果您在序列化之前更改了 baz 的值,然后进行序列化/反序列化,它会将 baz 重置为默认值“baz”,而不是您更改为的值。

class Foo {
    public $bar = 'bar';

    public $baz = 'baz';

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

$obj = new Foo();
$obj->baz = 'newbaz';

var_dump($obj);

$serialized = serialize($obj);
$unserialized = unserialize($serialized);
var_dump($unserialized);

You're defining an initial value of 'baz' for the $baz property, so when you unserialize, PHP recreated baz with that default value despite the fact that it's not part of the serialized object.
If you changed the value of baz before serializing, then serialize/unserialize, it will reset baz to that default value of 'baz', rather than to the value you had changed it to.

class Foo {
    public $bar = 'bar';

    public $baz = 'baz';

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

$obj = new Foo();
$obj->baz = 'newbaz';

var_dump($obj);

$serialized = serialize($obj);
$unserialized = unserialize($serialized);
var_dump($unserialized);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文