3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @template T */ final class ReadonlyReference { /** * @param T $value */ public function __construct( public readonly mixed $value ) {} } /** * @template T */ final class Reference { /** * @param T $value */ public function __construct( public mixed $value ) {} } /** * @param ReadonlyReference<list<int>> $foo */ function foo(ReadonlyReference $foo): void { foreach($foo->value as $s) { var_dump($s); } $foo->value[] = 1; } $ref = new ReadonlyReference([ 1, 2, 3 ]); foo($ref);
Output for 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
int(1) int(2) int(3) Fatal error: Uncaught Error: Cannot indirectly modify readonly property ReadonlyReference::$value in /in/ehYQh:35 Stack trace: #0 /in/ehYQh(42): foo(Object(ReadonlyReference)) #1 {main} thrown in /in/ehYQh on line 35
Process exited with code 255.
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.5 - 8.3.30
int(1) int(2) int(3) Fatal error: Uncaught Error: Cannot modify readonly property ReadonlyReference::$value in /in/ehYQh:35 Stack trace: #0 /in/ehYQh(42): foo(Object(ReadonlyReference)) #1 {main} thrown in /in/ehYQh on line 35
Process exited with code 255.
Output for 8.0.1 - 8.0.27
Parse error: syntax error, unexpected identifier "mixed", expecting variable in /in/ehYQh on line 11
Process exited with code 255.

preferences:
69.08 ms | 939 KiB | 4 Q