PHP5 Tutorial – __clone() method

January 1st, 2011 by laeeq | No comments

To clone an object means to create a duplicate of an object. With regular variables $a = $b means that a new variable $a gets created that contains the value of $b. This means that 2 variables get created.

With objects $obj2 = $obj1 does not mean that a new object i.e. $obj2 gets created. When we execute $obj2 = $obj1, the reference of $obj1 is assigned to $obj2. This means that $obj1 and $obj2 point to the same memory space. Look at the diagram below.

Lets look at an example where only references are assigned to another object:

class Customer {
private $name;public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}

$c1 = new Customer();
$c1->setName(“Stuart”);

$c2 = $c1; //only reference or memory assigned to $c2

$c2->setName(“Jhon”);

echo $c1->getName().”\n”;
echo $c2->getName().”\n”;

Output:
Jhon
Jhon

 

In the above example, $c2 has the reference of $c1; therefore, when you set a new name in the $c2 object – $c1 object changes as well. Therefore, when an object is assigned as a reference; changes made to one object are also reflected in the other.

Therefore, to create a new $obj2 object we must clone an object to create a new one. To clone an PHP5 Object a special keyword i.e. clone is used. Example below:

 

$obj2 = clone $obj1;

 

After the above line is executed $obj2 with a new memory space is created with the data members having the same value as that of $obj1. This is also referred to as shallow copy.

The above technique works with a class having data members that are of intrinsic type i.e. int, boolean, string, float, etc.. However, this technique will not work with a class that has a data member which is an object of another class. In such a scenario, the cloned object continues to share the reference of the data member object of the class that was cloned.

So, how do we resolve this issue? Doing a regular shallow copy won’t help us. To allow aggregated objects (i.e. data members that are objects of another class) to also get cloned properly we need to use the concept of ‘deep copy‘ as opposed to ‘shallow copy‘. To implement a ‘deep copy‘ you should implement the magic method __clone().

You could also provide the implementation of __clone() magic method even when you don’t have an aggregated object. You would want to do this for providing necessary clean up operations, conversions or validations.

Lets explore a very simple example of cloning intrinsic data types:

class Customer {
private $name;public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}

public function __clone() {
$c = new Customer();
$c->setName($this->name);
return $c;
}

}

$c1 = new Customer();
$c1->setName(“Stuart”);

$c2 = clone $c1; //new object $c2 created

$c2->setName(“Jhon”);

echo $c1->getName().”\n”;
echo $c2->getName().”\n”;
Output:
Stuart
Jhon

 

In the above example, observe the line where the statement $c2 = clone $c1 is executed. This is internally represented as $c2 = $c1.__clone(). However, you cannot explicitly call the __clone() method on an object as the __clone() is automatically called. Now that $c1 and $c2 are two individual objects, changes made to one object is not reflected in the other.

 

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Find Time Between Two Dates in PHP
  • Creating Objects in PHP5 Class
  • PHP5 Tutorial – Definition of a class attribute
  • PHP5 Tutorial – Creating a PHP5 Destructor
  • PHP5 Tutorial- Definition of Constructor
  • PHP5 Tutorial – magic method
  • PHP5 Tutorial – Magic Methods – __autoload() method
  • PHP5 Tutorial – magic method __call()
  • PHP5 Tutorial – Magic Methods – __isset() and __unset()
  • PHP5 Tutorial – get()) and __set() magic method
  • PHP5 Tutorial – __toString() method
  • PHP5 Tutorial – Polymorphism
  • PHP5 Tutorial – abstract class and interface
  • PHP5 Tutorial- Inheritance
  • PHP5 Tutorial – exception handling
  • PHP5 Tutorial – Final Class and Final Method
  • PHP5 Tutorial – Static Data Members and Methods
  • PHP5 Tutorial – Defining Class Constants
  • PHP5 Tutorial – instanceOf Operator
  • PHP5 Tutorial – $this variable
  •  

     

    1. No comments yet.
    1. No trackbacks yet.