Singleton
trait Singleton (View source)
This trait provides methods to manage a single instance of a class or multiple instances identified by a unique hash. It includes methods to get, create, set, and clear instances.
Methods:
getInstance(): self:Retrieves an existing instance or creates a new one.newInstance(): self:Creates a new instance and sets it as the current instance.setInstance(): void:Sets a specific instance, optionally identified by a hash.clearInstance(): void:Clears a specific instance or the default instance.clearAllInstances(): void:Clears all instances, including the default one.
Fields:
$instance: Static instance for classic singleton pattern.$instances: Array to store multiple instances by hash.
Methods
Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash
Create a new instance of the class and set it as the instance
Clear instance(s)
Clear all instances including the classic singleton instance
Details
static Singleton
getInstance(string|null $hash = null)
Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash
static Singleton
newInstance(string|null $hash = null)
Create a new instance of the class and set it as the instance
static void
setInstance(Singleton $instance, string|null $hash = null)
Set a specific instance
static void
clearInstance(string|null $hash = null)
Clear instance(s)
static void
clearAllInstances()
Clear all instances including the classic singleton instance