Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1022639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:29:46+00:00 2026-05-16T11:29:46+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:29:46+00:00Added an answer on May 16, 2026 at 11:29 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a simple cache using the dictionary class: private Dictionary<int, byte[]> cache
I've got a simple C class I have implemented, using function pointers in a
I have implemented a simple server-client script like this: Server: class Server(Protocol): def connectionMade(self):
I have implemented a simple linked list class and I would now like to
I have implemented a simple clock like so: - (void)runTimer { timer = [NSTimer
I have implemented a very simple method: private String getProfileName(String path) { String testPath
I have a simple composite component with an optional ajax listener implemented with the
I have implemented a simple ostream and streambuf class. For some reason, it crashes
I have implemented simple com.sun.net.httpserver.HttpServer application using one of examples in the internet. Server
We have implemented a simple chat room feature in Rails using Simple Ajax updates.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.