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

  • Home
  • SEARCH
  • 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 6735995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:04:41+00:00 2026-05-26T11:04:41+00:00

I have an object-oriented parent-child tree in PHP that I want to clone. The

  • 0

I have an object-oriented parent-child tree in PHP that I want to clone.
The difficult part is that the access to the tree is not always through the root, but sometimes through a child of the root, like this:

[Root]
  -- [Element1] START CLONE
       -- [Element3]
       -- [Element4]
  -- [Element2]
       -- [Element5]

So what I want to do is to clone the entire tree, by calling $new = clone $element1;

The __clone() method states that each of the children must also be cloned, and, if the illustrated situation occurs*, also the parent must be cloned.

* Root is explicitly set as parent in Element1, so the system can identify this situation and do something with it.

The problem is that, starting the clone operation from Element1, Root must also be cloned. The cloning procedure for Root prescribes that all child elements must be cloned and therefore the clone operation for Element1 is called again, which then repeats the same cloning procedure, producing an endless loop.

Furthermore, the Root will not contain the first clone of Element1, but it will produce its own clone to add as a child. Element1 will then have Root as its parent, but Root will not have the same Element1 as a child.

I hope I presented the problem in a clear way and that someone can help me find a solution.

EDIT:

Final solution:

/**
 * The $replace and $with arguments allow a custom cloning procedure. Instead of
 * being cloned, the original child $replace will be replaced by $with.
 */
public function duplicate($replace = null, $with = null) {
    // Basic cloning
    $clone = clone $this;
    // If parent is set
    if(isset($this->parent)) {
        // Clone parent, replace this element by its clone
        $parentClone = $this->parent->duplicate($this, $clone);
        $clone->parent = $parentClone;
    }

    // Remove all children in the clone
    $clone->clear();

    // Add cloned children from original to clone
    foreach($this->getChildren() as $child) {
        if($child === $replace)
            // If cloning was initiated from this child, replace with given clone
            $childClone = $with;
        else
            // Else duplicate child normally
            $childClone = $child->duplicate();

        // Add cloned child to this clone
        $clone->add($childClone);
    }

    return $clone;
}
  • 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-26T11:04:41+00:00Added an answer on May 26, 2026 at 11:04 am

    First of all, simplify your example:

    [Root]
      -- [Element1] START CLONE
           -- [Element3]
    

    Then differ between what you do, I think you have got three operations

    • public clone method.
    • A self-clone operation that returns a clone with children, but not the parent.
    • A single-clone operation that returns a clone w/o children.

    Code outside of your classes is using the public clone method. But the __clone() method must not use that method, otherwise you run into the circular looping problem you’ve described. So the __clone() implementation must use other methods.

    Add a cloneSelf and cloneSingle method to your class, make them protected, so inherited classes can call them but they are not publicly available.

    Then make use of them in the __clone() implementation:

    public function clone()
    {
        // clone the parent
        $parent = $this->getParent();
        $parentClone = $parent->cloneSingle();
    
        // clone all children of parent which includes $this
        $selfClone = NULL;
        foreach($parent->getChildren() as $child)
        {
            $childClone = $child->cloneSelf();
            if (!$selfClone && $child === $this)
                $selfClone = $childClone;
            $parentClone->addChild($childClone);
    
        }
    
        assert('$selfClone');
    
        return $selfClone;
    }
    
    public function __clone()
    {
        $message = 'Clone operator is not allowed, use clone() method instead.';
        throw new BadMethodCallException($message);
    }
    

    These methods will also help you to clone in case there is no parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always heard that C++ is not Object Oriented but rather C with
I have a need to create a library of Object Oriented PHP code that
I have a clue about Object Oriented Programming: I need to have a parent
I think I read somewhere that some modules only have object oriented interfaces (
I'm just starting with Object Oriented PHP and I have the following issue: I
There have been some questions about whether or not JavaScript is an object-oriented language.
I have an object-oriented MATLAB app that needs a GUI, and I'd like to
Object-Oriented programmers seem to have all the fun. Not only are they treated to
I have: existing object oriented native code API (non GUI) GUI application that works
I have an object oriented framework that uses a page design, where each page

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.