Categories
pattern

The singleton pattern in Symfony PHP Framework

The singleton pattern in Symfony is useful when we need to make sure we only have a single instance of a class for the entire request lifecycle in a web application.

<?php
 
/**
 * The Singleton class defines the `GetInstance` method that serves as an
 * alternative to constructor and lets clients access the same instance of this
 * class over and over.
 */
class Singleton
{
    /**
     * The Singleton's instance is stored in a static field. This field is an
     * array, because we'll allow our Singleton to have subclasses. Each item in
     * this array will be an instance of a specific Singleton's subclass. You'll
     * see how this works in a moment.
     */
    private static $instances = [];
 
    /**
     * The Singleton's constructor should always be private to prevent direct
     * construction calls with the `new` operator.
     */
    protected function __construct() { }
 
    /**
     * Singletons should not be cloneable.
     */
    protected function __clone() { }
 
    /**
     * Singletons should not be restorable from strings.
     */
    public function __wakeup()
    {
        throw new \Exception("Cannot unserialize a singleton.");
    }
 
    /**
     * This is the static method that controls the access to the singleton
     * instance. On the first run, it creates a singleton object and places it
     * into the static field. On subsequent runs, it returns the client existing
     * object stored in the static field.
     *
     * This implementation lets you subclass the Singleton class while keeping
     * just one instance of each subclass around.
     */
    public static function getInstance(): Singleton
    {
        $cls = static::class;
        if (!isset(self::$instances[$cls])) {
            self::$instances[$cls] = new static();
        }
 
        return self::$instances[$cls];
    }
 
    /**
     * Finally, any singleton should define some business logic, which can be
     * executed on its instance.
     */
    public function actualCode()
    {
        // put your actual code here
    }
}

In this example, we implemented the Singleton pattern – it has a private constructor that can only be called once and the single instance of the Singleton class will be stored in the instance variable.

Using the Singleton pattern in Symfony PHP Framework can be useful when you want to centrally store an object in your application.

The Singleton pattern is a good pattern to know because it is one of the most widely used patterns in the PHP Framework world.