How To Create A Singleton Class In PHP

A few weeks ago, I wrote a post about PHP Factory Patterns. The Singleton pattern also deals with the construction of objects but serves an entirely different purpose.

When creating PHP applications, we use multiple function calls and class initializations. Sometimes, when we’ve created an object, we want to be able to access that same object from multiple classes. This is exactly what the Singleton pattern aims to achieve. Rather than creating an instance of the object each time the class is initialized, classes using the Singleton pattern will simply return the first object you created, no matter how many times you attempt to create a new object.

How to set it up

class User
    // Hold an instance of the class
    private static $instance;

    // The singleton method
    public static function singleton()
        if (!isset(self::$instance)) {
            self::$instance = new __CLASS__;
        return self::$instance;

That’s the most basic form of a Singleton Pattern. There are two main elements that make up this pattern:

  • Static Instance Variable: This variable is declared as static and is therefore not accessed through an object. In a sense, a static variable is simply some information about the class that does not get reset whenever a new object is created. It is attached to a class, not an object. In the Singleton Pattern, this $instance variable will hold a copy of the first object you have created.
  • The Singleton Function: This is the function that will create a new object of that class and assign it to the static $instance variable. Before doing that however, using if (!isset(self::$instance)), the function will check whether the $instance variable has already been set or not. If it has, it simply ignores the next line and returns the saved object. This is the functionality that defines the Singleton Pattern.

Now, to create an object, we would simply do:

$oUser1 = User::singleton();
$oUser2 = User::singleton();
$oUser3 = User::singleton();

Can you guess what these three function calls will return? They will all return the exact same object. The first call will create the object and store it in $instance. The last two will detect that an object has already been created and simply return the object saved in $instance.

So why would we use it?

Like many other patterns, it’s often hard to define the exact scenarios when the Singleton pattern would be appropriate. Truth be told, there are often ways to get around doing the same thing by simply passing object instances in function parameters, especially when you would need to create multiple objects from the same class. It is important to keep in mind that this pattern can only be used if you want your class to produce just one object – no more.

In a sense, this pattern emulates some static class functionality (accessing the same class functions and variables from different objects) while still keeping the object oriented functionality in there. Like all the other patterns, you can always get by without them – Patterns are simply ways to make your life easier. If you’re developing a PHP application and come across a class which:

  1. Needs to be only initialized once – Only one object is ever created
  2. Needs to be accessed (with the same data) by several different functions and objects
  3. You want to use the best practices

…then that probably means you should use the Singleton Pattern.

Happy coding!

Don't forget to check out more posts from the Tutorial of the Week section.

blog comments powered by Disqus