Created
May 1, 2022 18:26
-
-
Save BlackScorp/de8e3d63f54fae912ce6fad4cf672116 to your computer and use it in GitHub Desktop.
testing deep clones
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 SubSubChild { | |
public function __construct(private string $value) | |
{ | |
} | |
} | |
class OtherObject{ | |
public function __construct(private SubSubChild $subSubChild) | |
{ | |
} | |
} | |
class SomeObject | |
{ | |
private string $property; | |
public function __construct(private OtherObject $child) | |
{ | |
} | |
public function getProperty(): string | |
{ | |
return $this->property; | |
} | |
public function setProperty(string $property): SomeObject | |
{ | |
$clone = clone $this; | |
$clone->property = $property; | |
return $clone; | |
} | |
} | |
$object = new SomeObject(new OtherObject(new SubSubChild('test'))); | |
$object2 = $object->setProperty('test'); | |
$object3 = $object2->setProperty('test2'); | |
var_dump($object2); | |
var_dump($object3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment