\Omega\Support\Singleton SingletonTrait

Singleton trait.

The SingletonTrait encapsulates a design pattern used to ensure that a class has only one instance and provides a global point of access to that instance. By incorporating this trait into a class, it enables the creation of a single instance of the class and restricts any subsequent instantiations to return the same instance.

Summary

Methods
Properties
Constants
getInstance
__clone
__wakeup
__sleep
No public properties found
No public constants found
No protected methods found
No protected properties found
No protected constants found
No private methods found
instances
No private constants found

Properties

$instances

$instances : static[]

Singleton instance.

Type

array<string|int, static> — Holds the singleton instances.

Methods

getInstance()

getInstance(string|null  $basePath = null) : static

Get the singleton instance.

This method returns the singleton instance of the class. If an instance doesn't exist, it creates one and returns it.

Parameters

string|null $basePath

Holds the Omega application base path or null.

Returns

static —

Return the singleton instance.

__clone()

__clone() : void

Clone method.

This method is overridden to prevent cloning of the singleton instance. Cloning would create a second instance, which violates the Singleton pattern.

Throws

\Omega\Support\Singleton\Exception\SingletonException

If an attempt to clone the singleton is made.

Returns

void —

__wakeup()

__wakeup() : void

Wakeup method.

This method is overridden to prevent deserialization of the singleton instance. Deserialization would create a second instance, which violates the Singleton pattern.

Throws

\Omega\Support\Singleton\Exception\SingletonException

If an attempt at deserialization is made.

Returns

void —

__sleep()

__sleep() : array

Sleep method.

This method is overridden to prevent serialization of the singleton instance. Serialization would create a second instance, which violates the Singleton pattern.

Throws

\Omega\Support\Singleton\Exception\SingletonException

If an attempt at serialization is made.

Returns

array —

Return the names of private properties in parent classes.