Singleton Pattern in PHP

Introduction

When your application has a class and the system only needs one instance of a class, and that instance is accessible throughout the program, and whenever the system only needs one instance of a class, but the object (instance) is used by a different application of a system then you can use a Singleton Pattern. Or in other words, if you want to build an application in PHP and the application implements a class that has only one instance, and this instance is to be accessible at the global level then you must use a singleton pattern of your application.

A singleton pattern is used to "Ensure a class has only one instance and provide a global point to retrieve it".

To do that, you must use the following procedure.

Step 1

Create a class, as in:

class
SingletonExample   { }

Step 2

Create a private static member variable, as in:


private static $instance;

Step 3

Define a constructor as private, as in:


private
function __construct()   { }

Step 4

Write a static function, which will be used to create an instance of this class and return it to calling method; see:

public static function singleton()

{

if (is_null(self::$instance)) {

$c = __CLASS__;

self::$instance = new $c;

}

return self::$instance;


Step 5

Create a Public function of this class, to printing some thing( basically this method is called by singleton), as in:

public
function GetMe()  { }

Step 6


Create an object of this class, as in:


$obj1=SingletonExample::singleton();


and now call the function of this class:

  $obj1->GetMe();
 

Example of Creating Singleton Pattern in PHP
 

<?php

 class SingletonExample

{

// Hold an instance of the class

private static $instance;
 

// A private constructor; prevents direct creation of object

private function __construct()

{

echo 'I am constructed. '."</br>";

}
 

// The singleton method

public static function singleton()

{

if (is_null(self::$instance)) {

$c = __CLASS__;

self::$instance = new $c;
 

//or self::$instance = new SingletonExample();

}

return self::$instance;

}
 

// Example method

public function GetMe()

{

echo 'Play with Singleton!';

}

}

$obj1->GetMe();

?>

Explanation


In the above example, first a class is created with the name "SingletonExample", then a private static member variable is created that holds a reference to the single instance that was created. After it is created a public static method with the name of "singleton". Basically public static indicates a reference to a single instance, so this singleton method checks if the instance is available or not. If the instance is available then it is returned, otherwise the new instance will be created (self::$instance = new SingletonExample()). Now when you create an instance of the class singleton, it simply calls singletonExample::singleton, so a new object is created and subsequently the public function GetMe is called.


Output

singleton-pattern-in-php.jpg

Advantages of the Singleton Pattern

  • High performance.

  • Singletons allow you to reuse code and control object state much easier.

  • Single guarantee that only one instance of the class exists.

  • The single instance will be globally accessible.

  • Reduces complexity.

Up Next
    Ebook Download
    View all
    Learn
    View all