__clone() magic method in oop

<?php

/*__clone() method used to create a clone of a object of class .. However there is a subtle difference between cloning a object of class or assigning a object of class to another .. When we assign $b = $c; that means $b is not a new object, $b use the same memory space like the $c use this called copy … whereas cloning an object make new memory space for the cloned object it is not a copy …

Example of cloning method is given below ..

*/
class b
{
public $a;
public function seta($a)
{
$this->a = $a;
}
public function geta()
{
echo $this->a;
}
public function __clone() /*this function is so much important cause when you use clone keyword to make a clone of an object its not mandetory to have a __clone mehtod within the class .. you can run the code making the full __clone() method within comment line and you will find out a successfull output having no problem ..*/
{
$an = new b();
$an->seta($a);
$an->geta();
}
}

$bol = new b();

$bol->seta(12);
$bol->geta();

$col = clone $bol;

$col->seta(13);
echo “<br/>”;
$col->geta();
?>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s