Base
in package
implements
ISingleton
AbstractYes
Abstract base class for implementing the Singleton design pattern.
This class provides the foundational behavior for singleton objects in the Neuron framework, ensuring only one instance of a class exists throughout the application lifecycle. It handles instance creation, storage, and serialization management.
Key features:
- Lazy initialization with automatic instance creation
- Abstract storage mechanism allowing different backends
- Serialization support for persistent singleton state
- Thread-safe instance retrieval in single-threaded environments
- Invalidation mechanism for controlled cleanup
Implementation requirements: Concrete subclasses must implement:
- instance(): Define storage mechanism (memory, file, cache, etc.)
- serialize(): Handle object serialization for persistence
- invalidate(): Implement cleanup and invalidation logic
Thread safety considerations: This implementation is safe for single-threaded PHP environments. For multi-threaded scenarios (e.g., Swoole, ReactPHP), consider implementing thread-safe storage mechanisms in subclasses.
Tags
Table of Contents
Interfaces
- ISingleton
- Singleton pattern.
Methods
- getInstance() : ISingleton|null
- instance() : mixed
- Gets the global object instance.
- invalidate() : void
- Clears the current global object.
- serialize() : void
- Writes the object data to the storage medium.
Methods
getInstance()
public
static getInstance() : ISingleton|null
Return values
ISingleton|nullinstance()
Gets the global object instance.
public
abstract static instance() : mixed
invalidate()
Clears the current global object.
public
abstract static invalidate() : void
serialize()
Writes the object data to the storage medium.
public
abstract serialize() : void