3v4l.org

run code in 300+ PHP versions simultaneously
<?php class NullableA { private function __construct() {} public ?string $string; public static function &string(?string $defaultValue = null) { $type = new self; $type->string = $defaultValue; return $type->string; // type is garbage collected } } class NullableB { private static $antiGarbageCollect = []; private function __construct() {} public ?string $string; public static function &string(?string $defaultValue = null) { $type = new self; self::$antiGarbageCollect[] = $type; $type->string = $defaultValue; return $type->string; } } $a =& NullableA::string(); $a = 123; var_dump($a); $b =& NullableB::string(); $b = 123; var_dump($b);
Output for 7.4.4 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
int(123) string(3) "123"
Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) in /in/ctEgn on line 6
Process exited with code 255.

preferences:
114.25 ms | 402 KiB | 171 Q