There can only be one president of a country at a time. The same president has to be brought to action, whenever duty calls. President here is singleton.
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
Singleton pattern is actually considered an anti-pattern and overuse of it should be avoided. It is not necessarily bad and could have some valid use-cases but should be used with caution because it introduces a global state in your application and change to it in one place could affect in the other areas and it could become pretty difficult to debug. The other bad thing about them is it makes your code tightly coupled plus mocking the singleton could be difficult.
To create a singleton, make the constructor private, disable cloning, disable extension and create a static variable to house the instance:
final class President{ private static $instance; private function __construct() { // Hide the constructor } public static function getInstance(): President { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } private function __clone() { // Disable cloning } private function __wakeup() { // Disable unserialize }}
Use Singleton when exactly one instance of a class is needed to coordinate actions across the system. For example, a single database connection, a logger, or a configuration manager.
When you need global access
When you need a global point of access to a resource, but want to ensure only one instance exists.
Before using Singleton, consider these alternatives:
Dependency Injection: Pass the instance as a parameter rather than using a global instance
Service Container: Use a container to manage single instances
Static Classes: If you don’t need instance state, consider using static methods
If you’re using modern PHP frameworks (Laravel, Symfony, etc.), they typically provide dependency injection containers that can manage singleton instances for you in a more testable way.
While Singleton should be used sparingly, some legitimate use cases include:
Database connection pools
Configuration managers
Logging services
Cache managers
Thread pools
However, in most modern applications, these are better handled through dependency injection containers rather than implementing the Singleton pattern directly.