3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); function &declare_int(int $value) { static $references = []; $valueWrapper = new class(5) { public function __construct(public int $value) { } }; $references[] = $valueWrapper; return $valueWrapper->value; } $intVariable = &declare_int(0); $intVariable = 42; // works $intVariable = 'test'; // fails
Output for 8.0.1 - 8.0.28, 8.1.0 - 8.1.17, 8.2.0 - 8.2.4
Fatal error: Uncaught TypeError: Cannot assign string to reference held by property class@anonymous::$value of type int in /in/T6GFS:19 Stack trace: #0 {main} thrown in /in/T6GFS on line 19
Process exited with code 255.
Output for 7.4.26
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/T6GFS on line 8
Process exited with code 255.

preferences:
39.4 ms | 406 KiB | 5 Q