All features

Cherry Blossom feature

Framework Traits: Singleton trait

Provides a shared instance and initialization lifecycle for a class.

Provides a shared instance and initialization lifecycle for a class.

Capabilities and extension points

Provides lazy shared-instance access and overridable initialization.

Holds the shared instance for the consuming class.

A subclass needing an independent instance must redeclare this property; otherwise it shares its parent's storage. Module replacements instead follow the canonical-resolution contract in WP_Module.

  • var object|null

Stores the instance before running the consuming class initializer.

To prevent code running multiple times,

always set instance first

Initializes the instance by registering its hooks.

Provides the hook-registration extension point for consuming classes.

Override to register callbacks. The base implementation does nothing.

Gets the shared instance, creating and initializing it on first access.

  • return self

Determines whether the call resolves through a subclass of the trait owner.

  • return bool Whether the runtime class differs from the class that uses this trait.

Prevents cloning a singleton instance.

  • throws \Exception Always.

Prevents restoring a singleton from serialized data.

  • throws \Exception Always.

Returns the canonical instance when the singleton object is invoked.

  • return object Singleton instance.

Source reference

This editable reference page is based on the PHP documentation in framework/traits/trait-singleton.php.