Home > PHP > PHP5 Tutorial – __clone Magic Method

PHP5 Tutorial – __clone Magic Method

In PHP 5 when you assign one object to another object creates a reference copy and does not create duplicate copy. This would create a big mess as all the object will share the same memory defined for the object. To counter this PHP 5 has introduced clone method which creates an duplicate copy of the object. __clone magic method automatically get called whenever you call clone methods in PHP 5.

Example – Without Object Cloning

< ?
class Animal
{
   public $name;
   public $legs;
 
   function setName($name)
   {
	$this->name = $name;
   }
 
   function setLegs($legs)
   {
	$this->legs = $legs;
   }
}
 
$tiger = new Animal();
$tiger->name = "Tiger";
$tiger->legs = 4;
 
$kangaroo = $tiger;
$kangaroo->name = "Kangaroo";
$kangaroo->legs = 2;
 
echo $tiger->name."---".$tiger->legs;
echo "<br />".$kangaroo->name."---".$kangaroo->legs;
?>

Output:
Kangaroo—2
Kangaroo—2

Your email:

 


Explanation:

  • Here i have created an $tiger object of Animal class
  • Created another variable $kangaroo and assigned $tiger to $kangaroo
  • After echo it print the details entered last because both the variables are referring to the same memory location

Example – Above Example With clone Function

< ?
class Animal
{
   public $name	;
   public $legs;
 
   function setName($name)
   {
	$this->name = $name;
   }
 
   function setLegs($legs)
   {
	$this->legs = $legs;
   }
 
   function __clone()
   {
	echo "<br />Object Cloning in Progress";
   }
}
 
$tiger = new Animal();
$tiger->name = "Tiger";
$tiger->legs = 4;
 
$kangaroo = clone $tiger;
$kangaroo->name = "Kangaroo";
$kangaroo->legs = 2;
 
echo "<br />".$tiger->name."---".$tiger->legs;
echo "<br />".$kangaroo->name."---".$kangaroo->legs;
?>

Output:
Object Cloning in Progress
Tiger—4
Kangaroo—2

Explanation:

  • Here i have created an $tiger object of Animal class
  • Created another variable $kangaroo having clone of $tiger. This calls the __clone magic method
  • After echo it print the details entered by individual object as both of them are referring to separate object and memory location
  • The above technique of cloning discussed is called shallow copy. There are other techniques called Deep Copy wherein you create duplicate copy of objects referring to other objects etc.


Custom Search

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: PHP Tags: ,
  1. gurpreet
    September 28th, 2008 at 12:01 | #1

    very nice tutorial and explanation. i helps us a lot.

    Thanks very much
    Gurpreet

  2. It_love
    August 25th, 2009 at 03:20 | #2

    Cam on da chia se, nhung ngu vai lon ai lai di dich ca code sang tieng viet.

  3. July 17th, 2010 at 03:48 | #3

    very gud tutorial…. helped me alot……………

  1. April 17th, 2010 at 22:37 | #1