3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Time { private static array $map = []; public float $seconds { get => $this->milliseconds / 1000.0; } public float $minutes { get => $this->seconds / 60.0; } private static function getValue(int $milliseconds): Time { // Attempt to get the value from the array, and if it exists, get the // value from the WeakReference, otherwise, create a new one $realValue = (self::$map[$milliseconds] ?? null)?->get() ?? new self($milliseconds); // Store the value in the array, even if another reference exists self::$map[$milliseconds] = WeakReference::create($realValue); return $realValue; } public function __destruct(){ // The values no longer exist, and we can delete the value from the array unset(self::$map[$this->milliseconds]); } private function __construct(public readonly int $milliseconds) {} public static function fromMilliseconds(int $milliseconds): Time { return self::getValue($milliseconds); } public static function fromSeconds(float $seconds): Time { return self::getValue($seconds * 1000); } public static function fromMinutes(float $minutes): Time { return self::getValue($minutes * 60000); } } var_dump(Time::fromSeconds(10) === Time::fromMilliseconds(10000));
Output for 8.3.9
Parse error: syntax error, unexpected token "{", expecting "," or ";" in /in/dVEOP on line 6
Process exited with code 255.

preferences:
166.31 ms | 1430 KiB | 7 Q