Design Pattern: Singleton Pattern in PHP

Singleton pattern is used to ensure that only an instance of a class is created. Each time an attempt to create an instance of the class will return the same instance.

Check details of Singleton Pattern at: Singleton Pattern Details.

Implementation of the Singleton pattern in PHP follows the same steps as Java and TypeScript.

Example #1: Database Connection

<?php

class DbConnSingleton {
    private static $singletonInstance;
    private $host;
    private $port;
    private $username;
    private $password;

    private function __construct($host, $port, $username, $password) {
        $this->host = $host;
        $this->port = $port;
        $this->username = $username;
        $this->password = $password;
    }

    public static function getInstance($host, $port, $username, $password) {
        if (self::$singletonInstance) {
            echo "\nUsing existing db connection instance";
            return self::$singletonInstance;
        }

        echo "\nCreating new db connection instance";
        self::$singletonInstance = new DbConnSingleton($host, $port, $username, $password);

        return self::$singletonInstance;
    }

    public function printConnectionDetails() {
        echo "\nhost:" . $this->host;
        echo "\nport:" . $this->port;
        echo "\nusername:" . $this->username;
        echo "\npassword:" . $this->password;
    }

    public function executeQuery($query) {
        echo "\nExecuting query: " . $query;
    }

}

$dbConnOne = DbConnSingleton::getInstance('loclahost', 1234, 'root', 'secret!pass');

echo "\nDB connection details for dbConnOne:";
$dbConnOne->printConnectionDetails();

$dbConnTwo = DbConnSingleton::getInstance('192.168.55.55', 2222, 'root2', 'secret!pass2');

echo "\nDB connection details for dbConnTwo:";
$dbConnTwo->printConnectionDetails();

Output

Creating new db connection instance

DB connection details for dbConnOne:
host:loclahost
port:1234
username:root
password:secret!pass

Using existing db connection instance

DB connection details for dbConnTwo:
host:loclahost
port:1234
username:root
password:secret!pass

Leave a Comment