[ create a new paste ] login | about

Link: http://codepad.org/Y1CRTeHC    [ raw code | output | fork ]

php.programmer - PHP, pasted on Apr 14:
<?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();
?>


Output:
1
2
3
4
5
after cloning
8
5


Create a new paste based on this one


Comments: