Neuron-PHP

Base 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
example
class DatabaseConnection extends Base
{
    private static $instance = null;

    public static function instance(): mixed
    {
        return self::$instance;
    }

    public function serialize(): void
    {
        self::$instance = $this;
    }

    public function invalidate(): void
    {
        self::$instance = null;
    }
}

// Usage
$db = DatabaseConnection::getInstance();

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

instance()

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

        
On this page

Search results