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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:26:27+00:00 2026-05-13T13:26:27+00:00

Can someone explain me what is Object Cloning in php? When should i use

  • 0

Can someone explain me

  • what is Object Cloning in php?

  • When should i use clone keyword in php?

  • 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-13T13:26:27+00:00Added an answer on May 13, 2026 at 1:26 pm

    Object cloning is the act of making a copy of an object. As Cody pointed out, cloning in PHP is done by making a shallow copy of the object. This means that internal objects of the cloned object will not be cloned, unless you explicitly instruct the object to clone these internal objects too, by defining the magic method __clone().

    If you don’t utilize the __clone method, the internal objects of the new object will be references to the same objecs in memory as the internal objects of the original object that was cloned.

    Consider these examples:

    // in this exampe the internal member $_internalObject of both objects
    // reference the same instance of stdClass in memory.
    class CloneableClass
    {
        private $_internalObject;
    
        public function __construct()
        {
            // instantiate the internal member
            $this->_internalObject = new stdClass();
        }
    }
    
    $classA = new CloneableClass();
    $classB = clone $classA;
    
    
    // in this exampe the internal member $_internalObject of both objects
    // DON'T reference the same instance of stdClass in memory, but are inividual instances
    class CloneableClass
    {
        private $_internalObject;
    
        public function __construct()
        {
            // instantiate the internal member
            $this->_internalObject = new stdClass();
        }
    
        // on clone, make a deep copy of this object by cloning internal member;
        public function __clone()
        {
            $this->_internalObject = clone $this->_internalObject;
        }
    }
    
    $classA = new CloneableClass();
    $classB = clone $classA;
    

    Use cases for cloning would for instance be a case where you don’t want outside objects to mess with the internal state of an object.

    Let’s say you have a class User with a internal object Address.

    class Address
    {
        private $_street;
        private $_streetIndex;
        private $_city;
        // etc...
    
        public function __construct( $street, $streetIndex, $city /* etc.. */ )
        {
            /* assign to internal values */
        }
    }
    
    class User
    {
        // will hold instance of Address
        private $_address;
    
        public function __construct()
        {
            $this->_address = new Address( 'somestreet', '1', 'somecity' /* etc */ );
        }
    
        public function getAddress()
        {
            return clone $this->_address;
        }
    }
    

    For arguments sake, let’s say you don’t want outside objects to mess with the internal Address of User objects, but you do want to be able to give them a copy of the Address object. The above example illustrates this. The getAddress method returns a clone of the address object to calling objects. This means that if the calling object alters the Address object, the internal Address of User will not change. If you didn’t give a clone, then the outside object would be able to alter the internal Address of User, because a reference is given by default, not a clone.

    Hope this all makes some sense.

    PS.:
    Be aware though, that if Address would also have internal objects, you would have to make sure Address makes a deep copy of itself on cloning (as per my second example of this post) by defining __clone() in Address. Otherwise you will get headaches of trying to figure out why your data is screwed.

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

Sidebar

Ask A Question

Stats

  • Questions 323k
  • Answers 324k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can build a Tree and then search will take… May 14, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer I don't notice anything obviously wrong with your code*. But… May 14, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer I totally prefer the first approach. The search box is… May 14, 2026 at 1:09 am

Related Questions

I'm fairly new to Java (been writing other stuff for many years) and unless
Yesterday I found this function: function clone(obj) { return typeof obj === 'undefined' ?
I am working on ruby rails project. I am using Rails 2.3.4 and ruby
Can someone explain the following code snippet for me? // Bind base object so
I am trying to test that a method to load a UI matrix is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.