Last active
May 22, 2019 17:48
-
-
Save vbarbarosh/271abba59595c8b757161ea5b404d9e4 to your computer and use it in GitHub Desktop.
php_class_clone_alt – Clone an object, then alter it a little https://codescreens.com
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 | |
| // Clone an object, then alter it a little | |
| // | |
| // Object Cloning | |
| // http://php.net/manual/en/language.oop5.cloning.php | |
| // | |
| // Magic Methods | |
| // http://www.php.net/manual/en/language.oop5.magic.php | |
| class State | |
| { | |
| public function __clone() | |
| { | |
| ... | |
| } | |
| # Clone an object, then alter it a little. | |
| public function alt($diff) | |
| { | |
| $clone = clone $this; | |
| foreach ($diff as $key => $value) { | |
| $clone->{$key} = $value; | |
| } | |
| return $clone; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment