6

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;
  }
Community
  • 1
  • 1
mahen3d
  • 7,047
  • 13
  • 51
  • 103
  • The [Prototype Design Pattern](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1) is also a good use case. – pce Aug 30 '12 at 11:06

3 Answers3

8

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!
Ivan Hušnjak
  • 3,493
  • 3
  • 20
  • 30
4

Perhaps startDate is an object.

Then. When you return clone $this->startDate - you get a full copy of the object. You can play with it, change values, call functions. And, until they affect database or filesystem - it's safe and the actual startDate object will not be modified.

But, if you just return object as is - you return only a reference. And any operation with object you do - you do this operation with the original object. Any change you make - it will affect that startDate.

This is only for objects and doesn't affect arrays, strings and numbers as they are value-type variables.

You should read more about value-type variables and reference-type variables.

Vitaly Dyatlov
  • 1,872
  • 14
  • 24
1

despite it is perfectley explained in another question (thanks for pointing this @gerald)

just a quick answer:

without a clone the function is returning a reference to the startDate Object. With clone its returning a copy.

if the returned object would be changed later, it only changes the copy and not the original, which might be used somewhere else also.

Community
  • 1
  • 1
Jan Prieser
  • 1,529
  • 9
  • 15
  • but the issue is in the fully object oriented design we need the actual object to be passed around not the clone object, isnt that so ? to me this design using clone objects is incrrect in a class when OO design practice uses.. – mahen3d Aug 30 '12 at 11:10