3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { private static $sealed = array(); public function __set($name, $value) { //if (isset(self::$sealed[spl_object_hash($this)])) { throw new Exception("can't touch this, nah nah nuh nahhh"); //} $this->$name = $value; } public function __get($name) { return $this->$name; } protected function finalize() { self::$sealed[spl_object_hash($this)] = true; // You could probably implement a __destruct to clean up and avoid leaking memory here, // but who gaf anyway this is just an example } } class ImmutableThingy { use Immutable; protected $foo; public function __construct($foo) { $this->foo = $foo; //$this->finalize(); } } $thing = new ImmutableThingy("bar"); var_dump($thing, $thing->foo); $thing->foo = 'baz'; var_dump($thing, $thing->foo);

preferences:
40.69 ms | 402 KiB | 5 Q