For singleton pattern just remember one word “Single”. What it means is, the class following this pattern should have only a single instance throughout the entire application scope. For example, if you created one object by suppose calling the new operator, then you should not be able to create any more new instances of the class in your application because it has already been created once. Then you say, how will we do it? The programmer can call new on the class again and it will simply give him another object!! Yes you are right, but by following the steps that a singleton class should employ the class will be immune to this problem. We will see how it’s done, but before that lets take a look at where it is useful.

When to use this pattern?

Suppose you need an object that you want to be responsible of a specific task throughout the application. The task can be something like holding the application settings or communicating with the database or something that has the below properties:

  • Its scope is in the application level, so that only one object may be required throughout the application.
  • Other objects may require the properties of the class performing the specific task and the properties should be same to every class that may require it.

For example an Employee class may also perform a specific task, but it may not be required by other classes except from a few and its implementation also varies from classes to classes throughout the object lifetime. However an ApplicationConfig class is application level. Its implementation and properties are same to every class that uses it. You create once instance of it and every other class can use that.

The instance of the singleton class acts like a global variable in your application. Oops!! Did I mention global variable? I know you will ask now, “yeah! What’s wrong with global variables? Can’t we just use a global variable to hold the properties that we may require throughout the class? For example a single global variable that represents the database instance. Why bother applying a design pattern when we can just use a keyword global?” Well, the use of global has some cons though. Let’s point out some below and then we will move to how to create a singleton class.

The Problem with using global variable:

The problem is that they are unprotected. You declare a global variable somewhere and at some other place of your application it gets overwritten. You will even don’t receive a warning for this. Without your knowing your application has gone BooM!! When you run it you notice undesired behavior and start looking for the problem. So without namespaces this can be a sever issue.

Secondly, if your classes depend upon global variables it will affect the reusability of the classes. If want to take out the class and use it in some other application, they you have to make sure that the same global variables are available there as well.

Implementing a Singleton Class:

For a singleton class at any given instance of time there should exist one and only one instance of the class anywhere in the application. To achieve this, the class should have the minimum properties mentioned below:

  • They must have a private constructor. It will prevent creating objects using new outside of the class.
  • A static variable to store the instance of the class. This is because we don’t want to create an instance of the class if already one is created.
  • Public static method to get the instance.

Example:

Lets create a class that will store the application configuration following the singleton pattern. We will name the class AppConfig. PHP has a magic method __clone() which allows to clone an object. We will implement a blank __clone() method as we don’t want  someone to create another instance of the class by cloning. The code will be like below:

To use the class in the application where needed, we will just call the getInstance() method. Implementing the singleton pattern we will be always assured that we are working on a single object.