3v4l.org

run code in 300+ PHP versions simultaneously
<?php final readonly class A { public string $foo; public function __construct(string $foo) { $this->foo = $foo; } public function withFoo(string $foo): static { $other = clone $this; $other->foo = $foo; return $other; } } $a = new A('y')->withFoo('x')->withFoo('z'); var_dump($a);
Output for 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Fatal error: Uncaught Error: Cannot modify readonly property A::$foo in /in/hnABl:12 Stack trace: #0 /in/hnABl(17): A->withFoo('x') #1 {main} thrown in /in/hnABl on line 12
Process exited with code 255.
Output for 8.3.0 - 8.3.30
Parse error: syntax error, unexpected token "->" in /in/hnABl on line 17
Process exited with code 255.

preferences:
149.05 ms | 1314 KiB | 6 Q