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 8761721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:21:24+00:00 2026-06-13T15:21:24+00:00

I am having a problem with using an object as a prototype for some

  • 0

I am having a problem with using an object as a prototype for some other objects.

The below code is expected to persist all instances of the object Container (visible in this code below is $module1 and $module2), however only the last one is persisted, and I assume it is due to the way I copy a prototype object.

Should I copy the prototype in some other way?

//Create module prototype
        $module = new Container();
        $module->setCompany($currentCompany);
        $module->setContainerType($typeModule);
        $module->setParent($entity);

        //Set the modules in use by this template (structure a bit ugly here, but makes it easier when dealing with the layout on other areas of the app)
        if ($size = $template->getModule1()) {
            $module1 = $module; //copy the prototype
            $module1->setName('Module1'); //Give a unique name
            $module1->setContainerSize($size); //Copy the size from the layoutTemplate
            $em->persist($module1); //Persist this module
            $layout->setModule1($module1); //Connect this container to become a module in the layout
        }

        if ($size = $template->getModule2()) {
            $module2 = $module; //copy the prototype
            $module2->setName('Module2'); //Give a unique name
            $module2->setContainerSize($size); //Copy the size from the layoutTemplate
            $em->persist($module2); //Persist this module
            $layout->setModule2($module2); //Connect this container to become a module in the layout
        }
  • 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-06-13T15:21:25+00:00Added an answer on June 13, 2026 at 3:21 pm

    You don’t really copy the object, you only create a new variable alias to the same object (they use the same underlying object). This would work with arrays, but not with objects.

    You can use clone to create a (shallow) copy of an object:

    $module1 = clone $module;
    

    Keep in mind though that $module and $module1 will have the same objects referenced. I,e if ContainerType is an object, the $module and $module1 will reference the same instance of ContainerType, which may or may not be what you want.

    You can read more about cloning in PHP5 here

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

Sidebar

Related Questions

I am having a problem creating new instances of an object. Using the below
I am having a problem using the signedCMS.decode routine. See the code below. The
I'm having a problem using deploy:deploy-file with snapshots I'd like some advice on please.
I'm having a problem using nested partials with dynamic form builder code (from the
I'm having a problem on Java using Iterator (LinkedList.iterator()) object. In a looping, I
I'm having a problem using some of the XPaths given to me by Chrome.
I have a code for inserting values in ASP.net using vb. I'm having problem
I'm having a problem serializing an object using Gson. @XmlRootElement class Foo implements Serializable
I am having problem using Date Object, when I try to parse unix-timestamp. example:
I am having problem getting some datetime conversions. I am actually using django and

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.