Skip to content

Instantly share code, notes, and snippets.

@vbarbarosh
Last active May 22, 2019 17:48
Show Gist options
  • Save vbarbarosh/271abba59595c8b757161ea5b404d9e4 to your computer and use it in GitHub Desktop.
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
<?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