Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active September 24, 2015 19:50
Show Gist options
  • Save fesor/fbf03248764f76d5b261 to your computer and use it in GitHub Desktop.
Save fesor/fbf03248764f76d5b261 to your computer and use it in GitHub Desktop.
<?php
class Foo extends Freezable {
}
$dto = new Foo();
$dto->somProp = 'someVal';
Freezable::freeze($dto);
$dto->somProp = 'override'; // RuntimeException
<?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