Glossary

Singleton

Singleton is a design pattern that guarantees only one instance of a class exists and provides a global access point to it. Classic uses: a configuration class, logger, or DB connection.

Implementation

class Config {
  private static ?self $instance = null;
  private function __construct() {}

  public static function getInstance(): self {
    self::$instance ??= new self();
    return self::$instance;
  }
}

Criticism

Singleton is one of the most criticised patterns. It introduces global state, complicates testing (hard to replace with a mock), and violates Dependency Inversion. In modern code it is replaced by registering the dependency as a singleton in an IoC container.