Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP clone keyword [duplicate]

Tags:

php

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;
  }
like image 346
mahen3d Avatar asked Aug 30 '12 10:08

mahen3d


People also ask

Does PHP support object cloning?

An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). $copy_of_object = clone $object; When an object is cloned, PHP will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references.

What is deep copy and shallow copy in PHP?

Deep copies duplicate everything. A deep copy, meaning that fields are de-referenced: rather than references to objects being copied, new copy objects are created for any referenced objects, and references to these placed in new object. Shallow copies duplicate as little as possible.

Which is a correct syntax of creating clone object?

The clone() method is defined in the Object class. Syntax of the clone() method is as follows: protected Object clone() throws CloneNotSupportedException.

Which magic function is used to clone an object?

The __clone() magic method is used to clone a class using a same new instance. If you notice closely, when we create a new object of class likeso, $class = new DevPeel(); a new object created of a classes and includes a index flag with the object.


1 Answers

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!
like image 124
Ivan Hušnjak Avatar answered Sep 19 '22 06:09

Ivan Hušnjak