Developer News

Cloning functions, a diferent approach dealing with classes.

kaotik  15-Sep-2009 09:54 7333 Reads   2 Comment(s) 
Learn how to clone your methods inside classes. A great way to pass and retrieve data.



This and other tutorials are also available at my site: http://www.kaotik.biz

This tutorial was originally written by:Hitesh Agrawal. Since it's no longer available online and such a good tutorial, I copied it to my site.

Cloning functions, a diferent approach dealing with classes.

In PHP 5 when you assign one object to another object, it creates a reference copy and does not create a 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 a duplicate copy of the object. __clone magic method automatically gets 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 
"
"
.$kangaroo->name."---".$kangaroo->legs;
?>


Output:
Kangaroo—2
Kangaroo—2

Explanation:

Here I have created a $tiger object of Animal class

Created another variable $kangaroo and assigned $tiger to $kangaroo

After echo it prints 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 
"
Object Cloning in Progress"
;
   }
}
 
$tiger = new Animal();
$tiger->name "Tiger";
$tiger->legs 4;
 
$kangaroo = clone $tiger;
$kangaroo->name "Kangaroo";
$kangaroo->legs 2;
 
echo 
"
"
.$tiger->name."---".$tiger->legs;
echo 
"
"
.$kangaroo->name."---".$kangaroo->legs;
?>

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

Explanation:

Here i have created an $tiger object of Animal class

Then created another variable $kangaroo having clone of $tiger. This calls the __clone magic method

After echo it prints 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.
Rating 0/5
Rating: 0/5 (0 votes)
Voting is disabled!


Login

Who's Online

416 user(s) are online (107 user(s) are browsing Publisher)


Members: 0


Guests: 416


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits

Categories