<?php
/* Sample code to show use of magic function __clone().
* author@ : http://royads.blogspot.com
* summery@ : clone is a keyword which call __clone() method. This method cant be called directly.
*/
class cls
{
protected $var;
function __construct($value)
{
$this->setValue($value);
}
public function setValue($value)
{
$this->var = $value;
}
public function getValue()
{
return $this->var;
}
public function __clone()
{
// This function gets executed automatically when clone of object is created.
// The code written in this function will work on properties of cloned object only.
// we can say this function is used to decide the post cloning operations.
$this->setValue(8);
}
}
$obj1 = new cls(5);
echo $obj1->getValue();
echo "\nafter cloning\n";
$objClone = clone $obj1;
echo $objClone->getValue();
echo "\n".$obj1->getValue();
?>