class Prototype { public $primitive; public $component; public $circularReference; public function __clone() { $this->component = clone $this->component; $this->circularReference = clone $this->circularReference; $this->circularReference->prototype = $this; } }Let's create another file we call ComponentWithBackReference.php. Note that the constructor won't be executed during cloning. If you have complex logic inside the constructor, you may need to execute it in the __clone method as well. The code for this file is:
class ComponentWithBackReference { public $prototype; public function __construct(Prototype $prototype) { $this->prototype = $prototype; } }At the top of the index.php file we add a couple of includes to inclued the files we just created.
include_once ('Prototype.php'); include_once ('ComponentWithBackReference.php');We now add some client code to our index.php file.
function clientCode() { $p1 = new Prototype; $p1->primitive = 245; $p1->component = new \DateTime; $p1->circularReference = new ComponentWithBackReference($p1); $p2 = clone $p1; if ($p1->primitive === $p2->primitive) { echo "Primitive field values have been carried over to a clone. Yay!<br/>"; } else { echo "Primitive field values have not been copied. Booo!<br/>"; } if ($p1->component === $p2->component) { echo "Simple component has not been cloned. Booo!<br/>"; } else { echo "Simple component has been cloned. Yay!<br/>"; } if ($p1->circularReference === $p2->circularReference) { echo "Component with back reference has not been cloned. Booo!\n"; } else { echo "Component with back reference has been cloned. Yay!\n"; } if ($p1->circularReference->prototype === $p2->circularReference->prototype) { echo "Component with back reference is linked to original object. Booo!\n"; } else { echo "Component with back reference is linked to the clone. Yay!\n"; } }add:
clientCode();to run this piece of code. Our result is:
Primitive field values have been carried over to a clone. Yay! Simple component has been cloned. Yay! Component with back reference has been cloned. Yay! Component with back reference is linked to the clone. Yay!Tell a friend "The Ray Code is AWESOME!!!" wikipedia
----------------------------------------------------------------------------------------------------
Find Ray on:
youtube
The Ray Code
Ray Andrade