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.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1 - 8.4.2
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.

preferences:
71.15 ms | 406 KiB | 5 Q