Categories
Software Engineering Programming

Singleton PHP Design Pattern

Design patterns are pretty much similar in all programming languages. So, I hope you can be benefited in any programming language after reading this article. In this article, I will try to demonstrate everything about the singleton pattern, and I will use PHP for code examples.

Singleton PHP design pattern is a Creational pattern. The vision of the singleton design pattern is to create only one instance of a class. So it means you can’t create multiple instances of a class. Also in the singleton pattern, there will be only one global access point to the object. Basically, a singleton pattern works like a global variable so there are some pros and cons that also come with this like a global variable. However, singleton design pattern is handy and getting popular more and more day by day. So I think it’s necessary for a developer to know about the singleton pattern. And, based on your project you have to decide if you are going to use singleton pattern or another one, so it depends on you and your application type.

Singleton can be recognized by a static creation method. Basically, there are some parts to make a singleton pattern. First of all, you can’t keep your __construct method public. There will be an static array to store the instance. Moreover, if you are using PHP or any other language then make sure object clone method should be resticted to private, in PHP it’s __clone. Also, you have to declare a instance calling method and it will work like a construct in this case. Lastly, you can also write some business logic if you want to execute it on its instance.

So now let’s see how we can make a Singleton class. And, I am going to use PHP for the example codes.

<?php

// Defining the namespace of the class
namespace DesignPattern\Singleton;

/**
* In place of the constructor, the Singleton class defines the 
* "getInstance" method, which enables clients to repeatedly access
* the same instance of this class.
**/

class Singleton
{
    // Hold the instance of the class.
    private static $instance = [];

    // Block outer access to construct.
    private function __construct() {}

    // Stop object cloning.
    private function __clone() {}
    
/**
 * The static method that manages access to the singleton 
 * instance is this one. It creates a singleton object on startup
 * and inserts it into the static field. The client existing 
 * object that was stored in the static field is returned on
 * subsequent runs. With this implementation, you can extend
 * the Singleton class while only retaining a single instance 
 * of each subclass.
*/
    public static function getInstance()
    {
         $class = static::class;

         if ( !isset( self::$instance[$class] ) ){
             self::$instance[$class] = new static();
         }

         return self::$instance[$class];
    }
    
     // Write your business logics here.
     public function businessLogic()
     {
         // Write codes here
     }
}

The above code is a singleton code. You can easily call the singleton just like this : Singleton::getInstance()

Leave a Reply

Your email address will not be published.