Created
October 2, 2010 02:46
-
-
Save wilmoore/607198 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
error_reporting(E_STRICT | E_ALL); | |
class ImmutableValueObject extends \ArrayObject | |
{ | |
public function __construct(array $data = array(), $flags = \ArrayObject::ARRAY_AS_PROPS) | |
{ | |
parent::__construct($data, $flags); | |
} | |
public function offsetSet($name, $value) | |
{ | |
throw new \RuntimeException('This object is immutable'); | |
} | |
public function offsetUnset($name) | |
{ | |
throw new \RuntimeException('This object is immutable'); | |
} | |
} | |
$f = new ImmutableValueObject(array('foo' => 'bar', 'baz' => 'bum')); | |
echo $f['foo'] . PHP_EOL; | |
echo $f->baz . PHP_EOL; | |
try { | |
$f['baz'] = 'bee'; | |
} catch (\Exception $e) { | |
echo 'Exception thrown as expected' . PHP_EOL; | |
} | |
try { | |
$f->baz = 'bee'; | |
} catch (\Exception $e) { | |
echo 'Exception thrown as expected' . PHP_EOL; | |
} | |
try { | |
$f->append('bee'); | |
} catch (\Exception $e) { | |
echo 'Exception thrown as expected' . PHP_EOL; | |
} | |
try { | |
unset($f['baz']); | |
} catch (\Exception $e) { | |
echo 'Exception thrown as expected' . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment