How to make a Singleton Design Pattern in PHP
Today, we'll discuss about Singleton Design Pattern of PHP. Let's make this pattern simple and very easy to use.
What is Singleton Design Pattern -
Singleton Pattern is a creational design pattern, which ensures that only one object of that class exists and provides a single point of access to it for any other code.
So, first when we've to use this Singleton pattern ? Problems are in some scenerios -
- If we don't want to recreat any object second time, then we need to use a way like Singleton.
- If there is a possibility of Memory optimization and one specific class must call one time, then we use singleton pattern.
Create a Singleton Design Pattern Class:
Let's start coding with creating a Singleton class.
<?php
class SingletonPatternExample
{
/**
* Singleton Instance
*
* @var SingletonPatternExample
*/
private static $instance;
/**
* Private Constructor
*
* We can't use the constructor to create an instance of the class
*
* @return void
*/
private function __construct()
{
// Don't do anything, we don't want to be initialized
}
/**
* Get the singleton instance
*
* @return SingletonPatternExample
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}
So, creating a singleton pattern class needs these staffs -
- A private instance object variable.
- Constructor should be private as we don't want to instantiate from any other class
- A static method like getInstance(). Which logics are above example -
- If No instance found, or instance is null
- It will return the instance
- Otherwise create an instance
- and Return the instance
Let's improve our Singleton class a little more to make it more stable -
<?php
class SingletonPatternExample
{
/**
* Singleton Instance
*
* @var SingletonPatternExample
*/
private static $instance;
/**
* Private Constructor
*
* We can't use the constructor to create an instance of the class
*
* @return void
*/
private function __construct()
{
// Don't do anything, we don't want to be initialized
}
/**
* Get the singleton instance
*
* @return SingletonPatternExample
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Private clone method to prevent cloning of the instance of the
* Singleton instance.
*
* @return void
*/
private function __clone()
{
// Don't do anything, we don't want to be cloned
}
/**
* Private unserialize method to prevent unserializing of the Singleton
* instance.
*
* @return void
*/
private function __wakeup()
{
// Don't do anything, we don't want to be unserialized
}
/**
* Do something
*
* @return void
*/
public function doSomething()
{
// Do something other stuffs
}
}
So, now we've called two more magic methods of PHP and make them private, so that never ever the class could be cloned or instanciate double times.
__wakeup()
- Prevents serialization__clone()
- Prevents duplication
How to call Our Singleton Class:
So calling Singleton class is simple -
<?php
// We can't instantiate like this - It will create an error
// $singletonPattern = new SingletonPatternExample();
// We have to call getInstance() static method
$singletonPattern = SingletonPatternExample::getInstance();
// Check the object
var_dump($singletonPattern);
// Call our doSomething() method.
$singletonPattern->doSomething();
So, we've seen, we never call a singleton class using new
.
$singletonPattern = new SingletonPatternExample();
It will throug an exception.
We've to use getInstance()
method to instantiate it.
// We have to call getInstance() static method
$singletonPattern = SingletonPatternExample::getInstance();
And we can access now any other method from the instance like -
$singletonPattern->doSomething();
Make a Trait of Singleton Class
and use it any class easily. Let's do that.
Create a trait called Singleton
in Singleton.php
<?php
trait Singleton
{
/**
* Singleton Instance
*
* @var Singleton
*/
private static $instance;
/**
* Private Constructor
*
* We can't use the constructor to create an instance of the class
*
* @return void
*/
private function __construct()
{
// Don't do anything, we don't want to be initialized
}
/**
* Get the singleton instance
*
* @return Singleton
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Private clone method to prevent cloning of the instance of the
* Singleton instance.
*
* @return void
*/
private function __clone()
{
// Don't do anything, we don't want to be cloned
}
/**
* Private unserialize method to prevent unserializing of the Singleton
* instance.
*
* @return void
*/
private function __wakeup()
{
// Don't do anything, we don't want to be unserialized
}
}
Now, in class, where we want to use this, use just like this -
<?php
class SingletonPatternExample
{
use Singleton;
/**
* Do something
*
* @return void
*/
public function doSomething()
{
echo 'Do Something...';
}
}
And You can create any Singletonable class using that trait very easily.
Like we want to make UserSeeder class to Singleton -
<?php
class UserSeeder
{
use Singleton;
/**
* Seed Users
*
* @return void
*/
public function seed()
{
echo 'Seeding...';
}
}
So, we can now easily re-use this Singleton trait across any of our classes and it would be very much helpful for our projects and we can follow the DRY(Do not Repeat Yourself) concept.
Tags: How to make a Singleton Design Pattern in PHP, Singleton pattern, PHP singleton design pattern, Design pattern PHP, How to create singleton design pattern in PHP, Singleton design pattern example, singleton in php
PHP If-else-elseif and Switch-case
PHP String Functions - All necessary String functions in PHP to manage strings better.
Popular Tutorials
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development