Created
May 20, 2021 13:20
-
-
Save gupta-shrinath/7a813e604fb2bc397ae888c3db6cab51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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