使用 SplObjectStorage 序列化对象树时出错

发布于 2024-09-16 02:42:23 字数 944 浏览 10 评论 0原文

我使用 SplObjectStorage 实现了一个简单的 Composite 模式,如上面的示例:

class Node
{
    private $parent = null;

    public function setParent(Composite $parent)
    {
        $this->parent = $parent;
    }
}

class Composite extends Node
{
    private $children;

    public function __construct()
    {
        $this->children = new SplObjectStorage;
    }

    public function add(Node $node)
    {
        $this->children->attach($node);
        $node->setParent($this);
    }
}

每当我尝试序列化 Composite 对象时,PHP 5.3.2 都会抛出分段错误。 仅当我向对象添加任意数量的任意类型的节点时,才会发生这种情况。

这是有问题的代码:

$node = new Node;
$composite = new Composite;
$composite->add($node);
echo serialize($composite);

虽然这个有效:

$node = new Node;
$composite = new Composite;
echo serialize($composite);

另外,如果我用 array() 而不是 SplObjectStorage 实现复合模式,所有运行也正常。

我做错了什么?

I have implemented a simple Composite pattern using SplObjectStorage, like the example above:

class Node
{
    private $parent = null;

    public function setParent(Composite $parent)
    {
        $this->parent = $parent;
    }
}

class Composite extends Node
{
    private $children;

    public function __construct()
    {
        $this->children = new SplObjectStorage;
    }

    public function add(Node $node)
    {
        $this->children->attach($node);
        $node->setParent($this);
    }
}

Whenever I try to serialize a Composite object, PHP 5.3.2 throws me a Segmentation Fault.
This only happens when I add any number of nodes of any type to the object.

This is the offending code:

$node = new Node;
$composite = new Composite;
$composite->add($node);
echo serialize($composite);

Although this one works:

$node = new Node;
$composite = new Composite;
echo serialize($composite);

Also, if I implement the Composite pattern with array() instead of SplObjectStorage, all runs ok too.

What I'm making wrong?

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

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

发布评论

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

评论(1

画离情绘悲伤 2024-09-23 02:42:33

通过设置Parent,您就拥有了循环引用。 PHP 将尝试序列化复合体,它的所有节点以及节点将依次尝试序列化复合体..繁荣!

您可以使用魔法 __sleep__wakeup() 方法在序列化时删除(或对其执行任何操作)父引用。

编辑:

看看将这些添加到Composite是否可以解决问题:

public function __sleep()
{
    $this->children = iterator_to_array($this->children);
    return array('parent', 'children');
}
public function __wakeup()
{
    $storage = new SplObjectStorage;
    array_map(array($storage, 'attach'), $this->children);
    $this->children = $storage;
}

By setting the Parent, you have a circular reference. PHP will try to serialize the composite, all of it's nodes and the nodes in turn will try to serialize the composite.. boom!

You can use the magic __sleep and __wakeup() methods to remove (or do whatever to the) the parent reference when serializing.

EDIT:

See if adding these to Composite fixes the issue:

public function __sleep()
{
    $this->children = iterator_to_array($this->children);
    return array('parent', 'children');
}
public function __wakeup()
{
    $storage = new SplObjectStorage;
    array_map(array($storage, 'attach'), $this->children);
    $this->children = $storage;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文