Index

tSingleton

MMO\core\classes · 1 symbol · generated 30 Sep 2026

trait

tSingleton

tSingleton.php:16

Trait to implement the singleton pattern: exactly one instance of the class exists per request, lazily created on first access and stored in the application's Configuration container, keyed by class name. The instance is built (and configured) from whatever config was Configuration::load-ed for this class name -- see e.g. static::DefaultConfig() on classes extending aModule.

trait MMO\core\classes\tSingleton
Namespace
MMO\core\classes
class MyService {use tSingleton; public string $greeting = 'hello';} MyService::Shared() === MyService::Shared(); // true: always the same instance

Index

Instantiate($config) static(Re)create the shared instance immediately, configured with $config merged into the class's existing config.
IsShared() static
Shared() staticGet the shared instance, creating (and configuring) it on first call.
Unshare($serialize) staticRemove the shared instance, so the next Shared() call creates a fresh one.

Methods4

Instantiate($config)

statictSingleton.php:61

(Re)create the shared instance immediately, configured with $config merged into the class's existing config.

static function Instantiate(array $config = []): staticUnlike a plain [`Shared()`](https://mmoframework.fr/documentation/mmo.core/php/classes/tSingleton.md#tsingleton-shared) call (which lazily builds the instance from whatever config was already loaded), this lets you supply/override config right before the instance is built.
$config = []arrayconfig values merged in before the instance is constructed
returnsstaticthe newly (re)created shared instance
MyService::Instantiate(['greeting' => 'yo'])->greeting; // 'yo'

IsShared()

statictSingleton.php:49
static function IsShared(): bool
returnsbooltrue if the shared instance has already been created (a call to Shared() would not need to construct a new one)
MyService::IsShared(); // false until the first MyService::Shared() call

Shared()

statictSingleton.php:22

Get the shared instance, creating (and configuring) it on first call.

static function Shared(): static
returnsstatic
MyService::Shared()->greeting; // 'hello'

Unshare($serialize)

statictSingleton.php:35

Remove the shared instance, so the next Shared() call creates a fresh one.

static function Unshare(bool $serialize = true): bool
See
serialize) and kept as the class's config, so the next instance created picks up where this one left off; if false, the instance is simply discarded and the next one starts from the class's original config
$serialize = trueboolif true (the default) and an instance currently exists, it is serialized first (via
returnsbooltrue if an instance actually existed and was removed
MyService::Unshare(); // true if MyService::Shared() had already been called; the next call rebuilds it