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);

preferences:
26.1 ms | 404 KiB | 5 Q