By default, the __clone method creates an object that has the same properties and methods as the original object. If you want to change the default content during cloning, you will overwrite (attribute or method) in the __clone.
A cloned method can have no parameters, but it also contains this and that pointer (that points to the object being copied). If you choose to clone yourself, you have to be careful to replicate any information you want your object to contain, from that to this, and if you use __clone to replicate, PHP will not perform any implicit replication, and here is an example of automating an object with a series ordinal:
Copy Code code as follows:
Class Objecttracker//Object Tracker
{
private static $nextSerial = 0;
Private $id;
Private $name;
function __construct ($name)//constructor
{
$this->name = $name;
$this->id = ++self:: $nextSerial;
}
function __clone ()//Clone
{
$this->name = "Clone of $this->name";
$this->id = ++self:: $nextSerial;
}
function getId ()//Get the value of the id attribute
{
Return ($this->id);
}
function GetName ()//Get the value of the Name property
{
Return ($this->name);
}
}
$ot = new Objecttracker ("Zeev ' s Object");
$ot 2 = Clone$ot;
Output: 1 Zeev ' s Object
Print ($ot->getid (). " " . $ot->getname (). "");
Output: 2 Clone of Zeev ' s Object
Print ($ot 2->getid (). " " . $ot 2->getname (). "");
?>