Skip to content

Instantly share code, notes, and snippets.

@MacDada
Created October 15, 2015 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacDada/ec921e6ffc1df26ab860 to your computer and use it in GitHub Desktop.
Save MacDada/ec921e6ffc1df26ab860 to your computer and use it in GitHub Desktop.
<?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