Singleton Pattern — PHP Implementation

Backend Developer
3 min readFeb 12, 2022

What the hell is Singleton?

Singleton Pattern

This pattern is used to ensure that there is only one instance of an object and that the same (and single instance) is called everywhere in your code base whenever you need that object.

First of all, each single object takes up memory space. Some of objects do not need to be created more than one. For example, let’s think that we need a database connection. If we create this object again on each client, we will occupy a significant amount of memory space, and we will occupy our database by creating more than one connection for no reason. In short, we will slow down the server both memory side and database side. In this condition what should we do is having one instance of db class. But how can we sure that we are creating one instance? Singleton pattern fix this issue for us.

The example above will be create a connection for every call for new DB class. This is non-singleton approach and will cause memory overflows. And also it will cause database deadlocks. Those are not wanted things for any developers. In prod environment this kind of mistake will be a disaster. I can hear your “Ok , ok ,ok! We understood what is not-singleton. For the sake of God tell us what is the Singleton?” thoughts so we can go on with the Singleton Pattern with same example

As you can see above we have a new static method named getInstance. With this method we make our class singleton. The another change is having private constructor in our class.

Why we have private constructor?

Without using private constructor every new creation of class will be create a new instance of class which is make performance issues for us. When we make it private no one can create new instance of this class and we can be sure for that.

Why we add static method?

We have a private constructor so how we init our class? We should have a static method which create an instance if there is no instance of this class and if there is one it will return that instance. So we can be sure that we have only one single instance from this class.This is also fixing first principle of SOLID (Single Responsibility Principle) .

// All the variables point to the same object.
$object1 = DB::getInstance();
$object2 = DB::getInstance();
$object3 = DB::getInstance();

Every variable will point our same DB instance. No more unwanted database connection, or memory usage of non singleton DB class. When php engine create $object1 variable we will create a new instance of DB class, because till there we did not init DB class. But in $object2 and 3 will be use same instance of created when $object1 is created.

Conclusion

We should decide when we are creating a new class if we need that with single instance or multiple instance. And prepare that class or object in which way needed. Hope i can explain this pattern in such a good way. Clap if you like it and please add your comments to make my articles better. Thank you for time of reading.

--

--