In this tutorial, I am going to discuss what is singleton design pattern, Why and when to use singleton design pattern in php.
What is Singleton Design Pattern?
In Singleton Design Pattern, we ensure a class has only one and only one instance and that instance can be accessed globally. The Singleton design pattern restricts the instantiation of a class to one object.
How to Create Singleton Class in PHP
To create a Singleton class we need
1. Static member variable – It acts as a placeholder for the instance of that class
2. Private Constructor – Private constructor prevents direct instantiation of a class.
3. Prevent object cloning by making clone magic method private.
4. Create a static method for global access.
Singleton Design Pattern in PHP
We have discussed some important points regarding singleton design pattern, let’s implement this in our class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class Singleton { private static $_instance = null; /* Prevent direct instantiation of a class. */ private function __construct() {} /* Prevent object cloning. */ private function __clone() {} /* Static method to provide global access point. */ public static function getInstance() { if(self::$_instance == null) { self::$_instance = new Singleton(); } return self::$_instance; } /* Prevent unserializing of singleton instance */ private function __wakeup() {} } $obj1 = Singleton::getInstance(); $obj2 = Singleton::getInstance(); var_dump($obj1); var_dump($obj2); /* Output - Only single instance is created object(Singleton)#1 (0) { } object(Singleton)#1 (0) { } */ |
In this example, I have created two variable which is pointing to the same object. When first time we call getInstance() method it will create a new object. After that any subsequent call will only return the instantiated object.
Let’s create a simple database connection class using Singleton Design pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php class DatabaseConnection { private $_connection; private static $_instance; private $_dbhost = "HOST" private $_dbusername = "USERNAME"; private $_dbpassword = "PASSWORD"; private $_dbdatabase = "DATABASE"; private function __construct() { $this->_connection = new mysqli($this->_dbhost, $this->_dbusername, $this->_dbpassword, $this->_dbdatabase); /* When unable to connect to MySql */ if(mysqli_connect_error()) { trigger_error("Connection Failed" . mysqli_connect_error(), E_USER_ERROR); } } public static function getInstance() { /* If an instance is not created. */ if(self::$_instance == null) { self::$_instance = new self(); } return self::$_instance; } /* Prevent object cloning. */ private function __clone() { } /* Get connection. */ public function getConnection() { return $this->_connection; } } |
NOTE – This design pattern is very useful when we required only a single instance of a class for the entire request lifecycle in a web application. You can use this design pattern for your logger class, configuration class etc. where we need only a global access point.