3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { final public function __construct() { //DO shit var_dump("In trait constructor"); } 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'; public function __construct() { var_dump("In class constructor"); // What the fuck ever broh } } $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.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
string(20) "In class constructor" 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/HHLvO:11 Stack trace: #0 /in/HHLvO(35): ImmutableThingy->__set('foo', 'baz') #1 {main} thrown in /in/HHLvO on line 11
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
string(20) "In class constructor" 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/HHLvO:11 Stack trace: #0 /in/HHLvO(35): ImmutableThingy->__set('foo', 'baz') #1 {main} thrown in /in/HHLvO on line 11
Process exited with code 255.

preferences:
153.9 ms | 402 KiB | 231 Q