Elektrine lite

← Feed

@nyamsprod@phpc.social

Post #3859989

2026-07-16 13:57 UTC

@ramsey@phpc.social final class readonly Duration { public function __construct(public $microseconds) {} public static function zero(): self { static $instance = null; $instance ??= new self(0); return $instance; } } The "zero" instance is a singletion declared once and because of the readonly it is safe to use, only once instance will be created

Replies (1)

  • @nyamsprod@phpc.social 2026-07-16 13:59

    @ramsey@phpc.social final class Duration { public int $microseconds { get => $this->ticks } public function __construct(private readonly $ticks) {} public static function zero(): self { static $instance = null; $instance ??= new self(0); return $instance; } } because of the hooks I loose the readonly property on the class and "singleton" nature ... now the in-memory cache no longer works it is better to return each time a new instance

    Open ##3860080