3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Thing { public function __construct(public readonly string $something) {} public function withSomething(string $something): self { $instance = clone $this; $instance->something = $something; return $instance; } } $thing = new Thing('hello'); $thing = $thing->withSomething('goodbye');
Output for 8.1.0 - 8.1.9
Fatal error: Uncaught Error: Cannot modify readonly property Thing::$something in /in/og8bn:10 Stack trace: #0 /in/og8bn(18): Thing->withSomething('goodbye') #1 {main} thrown in /in/og8bn on line 10
Process exited with code 255.
Output for 8.0.1 - 8.0.22
Parse error: syntax error, unexpected identifier "string", expecting variable in /in/og8bn on line 5
Process exited with code 255.
Output for 7.4.0 - 7.4.30
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/og8bn on line 5
Process exited with code 255.

preferences:
194.05 ms | 1395 KiB | 68 Q