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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:43:44+00:00 2026-05-27T12:43:44+00:00

Basically, I need to store past versions of this object in its current state

  • 0

Basically, I need to store past versions of this object in its current state in an array. Something like:

 public class MyClass{
     $n = 0;
     $array = array()

     storeOldVersion(){
          $this->array[] = clone $this;
     }
 }

I know I do something like “clone $this->n”, but how can I literally clone the MyClass object itself, and store it into an array the MyClass object holds?

Thanks

  • 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-27T12:43:45+00:00Added an answer on May 27, 2026 at 12:43 pm

    What you proposed actually works, except you had some incorrect code. The following version works:

    class MyClass{
         protected $n = 0;
         public $array = array();
    
         public function storeOldVersion(){
              $this->array[] = clone $this;
              $this->n = 2;
         }
     }
    
    $a = new MyClass();
    $a->storeOldVersion();
    
    echo "<pre>";
    var_dump( $a ); // retuns class with n = 2
    var_dump( $a->array ); // return array with one class having n = 0
    

    A word of warning though

    If you call storeOldVersion() more than once, as is, it will recursively clone the “array” that contains other clones and your object could get pretty massive exponentially. You should probably unset the array variable from the clone before storing it in the array..

    e.g.

    $clone = clone $this;
    $clone->array = array();
    $this->array[] = $clone;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked EVERYWHERE for this, can't find anything. I basically need to store an
I need a way to store an int for N columns. Basically what I
I basically need to show a wait window to the user. For this i
I basically need to check if there is an easier way to do this
I'm implementing a client-server model for an online store. Basically I need to retrieve
I need to store a Dictionary in my asp .net application. This dictionary is
I need to store an array of user created objects (a user enters data
I need to serial an object and store it on disk. I used Java's
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
basically need to change the value that - admin_url() returns any idea?

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.