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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:38:57+00:00 2026-06-10T22:38:57+00:00

Possible Duplicate: what is Object Cloning in php? I am studying a existing framework

  • 0

Possible Duplicate:
what is Object Cloning in php?

I am studying a existing framework which uses a “clone” keyword a lot, not sure whether this is a good idea to do this ? i dont really understand the need to use the ‘clone’ keyword.

for example have look at this coding

i.e

  public function getStartDate ()
  {
    return clone $this->startDate;
  }

to me this function should be like below, i dont see the need of the clone.

  public function getStartDate ()
  {
    return $this->startDate;
  }
  • 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-10T22:38:59+00:00Added an answer on June 10, 2026 at 10:38 pm

    Reason for using clone is that PHP when working with object always returns object as a reference, not as a copy.

    That is why when passing object to a function you don’t need to specify it with & (reference):

    function doSomethingWithObject(MyObject $object) { // it is same as MyObject &object
       ...
    }
    

    So in order to get object copy you have to use clone keyword
    This is an example on how objects are handled by php and what clone does:

    class Obj {
        public $obj;
        public function __construct() {
            $this->obj = new stdClass();
            $this->obj->prop = 1; // set a public property
        }
        function getObj(){
            return $this->obj; // it returns a reference
        }
    }
    
    $obj = new Obj();
    
    $a = $obj->obj; // get as public property (it is reference)
    $b = $obj->getObj(); // get as return of method (it is also a reference)
    $b->prop = 7;
    var_dump($a === $b); // (boolean) true
    var_dump($a->prop, $b->prop, $obj->obj->prop); // int(7), int(7), int(7)
    // changing $b->prop didn't actually change other two object, since both $a and $b are just references to $obj->obj
    
    $c = clone $a;
    $c->prop = -3;
    var_dump($a === $c); // (boolean) false
    var_dump($a->prop, $c->prop, $obj->obj->prop); // int(7), int(-3), int(7)
    // since $c is completely new copy of object $obj->obj and not a reference to it, changing prop value in $c does not affect $a, $b nor $obj->obj!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Fully Object Oriented framework in PHP I am frustrated with CodeIgniter about
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: Object reference not set to an instance of an object errors when
Possible Duplicate: Why is object not dealloc'ed when using ARC + NSZombieEnabled I've got
Possible Duplicate: How to determine an object's class (in Java)? Java determine which class
Possible Duplicate: Calling closure assigned to object property directly Why this is not possible
Possible Duplicate: Delete object and all its child objects in Entity Framework? This code:
Possible Duplicate: where we use object operator “->” in php In PHP 5, what
Possible Duplicate: Cache Object in PHP without using serialize So I have built a
Possible Duplicate: In PHP can someone explain cloning vs pointer reference? According to http://php.net/manual/en/language.oop5.references.php

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.