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.33, 8.2.22 - 8.2.29, 8.3.5 - 8.3.25, 8.4.1 - 8.4.12
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:
48.72 ms | 408 KiB | 5 Q