Last active
September 24, 2015 19:50
-
-
Save fesor/fbf03248764f76d5b261 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 Foo extends Freezable { | |
} | |
$dto = new Foo(); | |
$dto->somProp = 'someVal'; | |
Freezable::freeze($dto); | |
$dto->somProp = 'override'; // RuntimeException |
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 | |
abstract class Freezable | |
{ | |
private $attributes; | |
private $isFreezed; | |
public function __get($name) { | |
return $this->attributes[$name]; | |
} | |
public function __set($name, $value) { | |
if ($this->isFreezed) throw new RuntimeException(‘DTO is freezed!’); | |
$this->attributes[$name] = $value; | |
} | |
public static function freeze(Freezable $dto) { | |
$dto->isFreezed = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment