3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { public function __set($name, $value) { throw new Exception("can't touch this, nah nah nuh nahhh"); } public function __get($name) { return $this->$name; } } class ImmutableThingy { use Immutable; protected $foo = 'bar'; } $thing = new ImmutableThingy; var_dump($thing, $thing->foo); $thing->foo = 'baz'; var_dump($thing, $thing->foo);
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
object(ImmutableThingy)#1 (1) { ["foo":protected]=> string(3) "bar" } string(3) "bar" Fatal error: Uncaught Exception: can't touch this, nah nah nuh nahhh in /in/NO5s6:5 Stack trace: #0 /in/NO5s6(23): ImmutableThingy->__set('foo', 'baz') #1 {main} thrown in /in/NO5s6 on line 5
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
object(ImmutableThingy)#1 (1) { ["foo":protected]=> string(3) "bar" } string(3) "bar" Fatal error: Uncaught exception 'Exception' with message 'can't touch this, nah nah nuh nahhh' in /in/NO5s6:5 Stack trace: #0 /in/NO5s6(23): ImmutableThingy->__set('foo', 'baz') #1 {main} thrown in /in/NO5s6 on line 5
Process exited with code 255.

preferences:
198.75 ms | 402 KiB | 219 Q