Skip to content

Instantly share code, notes, and snippets.

@texdc
Created December 15, 2014 21:53
Show Gist options
  • Save texdc/9f6b59f38e6270387aa3 to your computer and use it in GitHub Desktop.
Save texdc/9f6b59f38e6270387aa3 to your computer and use it in GitHub Desktop.
<?php
class A
{
private $a;
public function __construct($a)
{
$this->a = $a;
}
}
class B extends A
{
/**
* Use php's magic method to enforce immutability
*/
public function __set($property, $value)
{
throw new RuntimeException(self::class . ' is immutable');
}
}
$a = new A(1);
$b = new B(2);
$a->b = $b;
var_dump($a);
try {
$b->c = 3;
} catch (RuntimeException $re) {
var_dump($b);
}
@texdc
Copy link
Author

texdc commented Dec 15, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment