PHP – singleton design pattern

TipoIT - author
Posted by : Darko Borojevic | contact me | go home

PHP singleton design pattern example

I will try to explain this as simple as possible. Singleton design pattern takes in a single class which is responsible for creating an object, while still making sure that only one instance of that object gets created. This class provides a way to access this single object which can be accessed directly without a need to create new object of the base class. Singleton design pattern is one of more controversial patterns in software development, but still widely used. Singleton class uses a static variable and a static getInstance() method which creates one object instance of itself. Singleton makes the default constructor private in order to prevent other objects from using the new operator with the Singleton class and the static creation method calls the private constructor to create the object and save it in a static field. So the same object is always returned.

PHP singleton pattern – definition script

php singleton design pattern

PHP singleton pattern – execution script

php singleton pattern test

The constructor __construct() is declared as private to prevent creation of a new instance outside of the class via the new operator. Magic method __clone() is declared as private and empty to prevent cloning of an instance with the clone operator. The magic method __wakeup() is declared as private and empty to prevent unserializing of an instance with the global function unserialize(). These three functions build the core of our Singleton pattern here. A new instance is created via late static binding in the static creation method getInstance(), with the help of static keyword. This allows the subclassing of the class DB. The Singleton pattern is useful when we need to make sure we only have a single instance of a base class for the entire request lifecycle in a web application. This typically occurs when we have global objects such as a database config class, for example, or a shared resource such as an event queue. Sometimes we need to implement such a class which allows us to create only one object of it. There can be many use cases where we can apply this design philosophy. For example that can include configuration classes, session manager classes, database connection class, and many more. We can’t control how developers use the base framework of our choice but we can design our base classes in such a way that it is not able to create multiple objects of a certain class. Instead it will return already created object if such exists. This is the case where we should consider developing a Singleton pattern for our base classes.

Singleton design pattern – visual representation


You can’t test Singletons?

However, it is worth to note that developers should be cautious when using the Singleton, because by its design it introduces global state into our application, which is reducing code testability. In majority of cases where this is a problem, dependency injection can be used in place of a Singleton class. Using dependency injection means that the object using the shared or global resource requires no knowledge of a concretely defined base class, which can maybe solve the testability problem. Global state can potentially be very bad because any code distributed within the application can change its value. So at the time of debugging it can be really hard to find which portion of the code has created the current stage of a global variable. So you see, Singleton is not a good idea if you are doing unit testing of your application, and it’s generally accepted that not performing unit testing for enterprise level applications is not a good idea.

Posted on: September 25, 2019

Print article Email article