Created
October 15, 2015 17:20
-
-
Save MacDada/ec921e6ffc1df26ab860 to your computer and use it in GitHub Desktop.
This file contains 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 Updatable | |
{ | |
private $field1; | |
private $field2; | |
public function __construct($field1, $field2) | |
{ | |
$this->field1 = $field1; | |
$this->field2 = $field2; | |
} | |
public function updateFrom(Updatable $otherUpdatable) | |
{ | |
// opcja 1: dynamicznie | |
// + prosta pętla po polach | |
// - jak dojdzie nowe pole, które nie powinno być kopiowane to będzie zonk | |
foreach ($this as $field => $value) { | |
if ($this->$field !== $otherUpdatable->$field) { | |
$this->$field = $otherUpdatable->$field; | |
} | |
} | |
// opcja 2: dynamicznie z #czarnolisto | |
// + prosta pętla po polach z wykluczaniem, | |
// lista pól może polecieć np jako stała, | |
// żeby była od razu widoczna po wejściu do pliku | |
// - nadal może być zonk jak ktoś zapomni zaktualizować listę | |
$excludeFields = ['field2']; | |
foreach ($this as $field => $value) { | |
if ($this->$field !== $otherUpdatable->$field && !in_array($field, $excludeFields)) { | |
$this->$field = $otherUpdatable->$field; | |
} | |
} | |
// opcja 3: biała lista | |
// + prosta pętla po polach | |
// - trzeba pamiętać o dołożeniu każdego nowego pola | |
$copyFields = ['field1']; | |
foreach ($copyFields as $field) { | |
if ($this->$field !== $otherUpdatable->$field) { | |
$this->$field = $otherUpdatable->$field; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment