Singleton
Raxos\Foundation\Util\Singleton is a static registry that keeps at most one instance per class name. It backs the global singleton() function and Option::none().
See the Singleton concept page for a guided introduction.
Signature
php
namespace Raxos\Foundation\Util;
final class SingletonMethods
php
public static function get(string $class): objectReturns the existing instance, or creates one with a plain constructor call and stores it.
php
public static function getOrNull(string $class): ?objectReturns the existing instance, or null if none was created yet.
php
public static function has(string $class): boolReturns true if an instance for the class already exists.
php
public static function make(string $class): objectForces creation of a new instance with a plain constructor call and stores it.
php
public static function register(string $class, callable $setup): objectReturns the existing instance, or creates one using the given factory callable.
Example
php
<?php
declare(strict_types=1);
use Raxos\Foundation\Util\Singleton;
$service = Singleton::get(MyService::class);
$configured = Singleton::register(
MyService::class,
static fn() => new MyService($config)
);