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 git.master, rfc.property-hooks
bool(true)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
144.98 ms | 1456 KiB | 7 Q