Skip to content

Instantly share code, notes, and snippets.

@gupta-shrinath
Created May 20, 2021 13:20
Show Gist options
  • Save gupta-shrinath/7a813e604fb2bc397ae888c3db6cab51 to your computer and use it in GitHub Desktop.
Save gupta-shrinath/7a813e604fb2bc397ae888c3db6cab51 to your computer and use it in GitHub Desktop.
<?php
class Obj
{
public $id;
public $size;
public $color;
function __construct($id, $size, $color)
{
$this->id = $id;
$this->size = $size;
$this->color = $color;
}
function __clone()
{
$green = $this->color->green;
$blue = $this->color->blue;
$red = $this->color->red;
$this->color = new Color($green, $blue,
$red);
}
}
class Color
{
public $green;
public $blue;
public $red;
function __construct($green, $blue, $red)
{
$this->green = $green;
$this->blue = $blue;
$this->red = $red;
}
}
$color = new Color(23, 42, 223);
$obj1 = new Obj(23, "small", $color);
$obj2 = clone $obj1;
$obj2->id++;
$obj2->color->red = 255;
$obj2->size = "big";
echo "<pre>";print_r($obj1);
echo "<pre>";print_r($obj2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment