Design Patterns: The Singletons Pattern

Follow the series topic regarding to Design Patterns Overview, today we will dive in to Singletons Pattern to understand clearly about it. Also checkout the design pattern series

Singletons Pattern’s definition

According to Wikipedia document:

In software engineering, the singleton pattern is a pattern in design patterns book that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

In simple form:
A Singleton is a programming design pattern that ensures a single instance of a class across an application.

Singletons Pattern Overview

What is the intent of Singletons Pattern?

As the definition, we nearly get the intent. We will summary as below

  • Ensure a class has only one instance for the entire request life-cycle in a web application.
  • Provide a global point of access to it.

When we use Singletons Pattern?

Singletons is mostly used in these scenarios

  • Logger Classes
  • Configuration Classes
  • Accessing resources in shared mode (E.g: an event queue)
  • Factories implemented as Singletons

An Example Singletons Pattern

class ExampleSingletonClass      
 * @var Singleton The reference to *Singleton* instance of this class        
 private static $_instance = null;       
 * Protected constructor to prevent creating a new instance of the       
 * *Singleton* via the `new` operator from outside of this class.        
 protected function __construct() { }        
 * Private __clone method to prevent cloning of the instance of the      
 * *Singleton* instance.         
 * @return void      
 private function __clone() { }      
 * Private __wakeup method to prevent unserializing of the *Singleton*       
 * instance.         
 * @return void      
 private function __wakeup() { }         
 /* Have a single globally accessible static method */       
 public static function makeInstance()       
 if( null === self::$_instance ) {       
 self::$_instance = new ExampleSingletonClass();         
 return self::$_instance;        
 /* Now we are all done, we can now proceed to have any other method logic we want*/         
 //a simple method to echo something         
 public function HelloWorld()        
 echo 'Hello, this is a singleton example';      
// Testing some calls to that class      
$obj1 = ExampleSingletonClass::makeInstance();       
$obj2 = ExampleSingletonClass::makeInstance();       
$obj3 = ExampleSingletonClass::makeInstance();       

In the code above, the singleton pattern uses a static variable and the static creation method makeInstance(). Explanation in details:

  • The constructor __construct() prevents creating a new instance outside of the class by the new operator.
  • The magic method __clone() prevents cloning of an instance of the class by the clone operator.
  • The magic method __wakeup() prevents unserializing of an instance of the class by the global function unserialize().
  • The mothods __construct, __clone, __wakeup are part of the famous PHP Magic Methods
  • A new instance is created by method makeInstance(). This allows the subclassing of the Singleton class in the example.

You can notice in the above code that makeInstance method ensures that only one instance of the class is created. The constructor can not be accessible from the outside of the class to ensure the only way of instantiating the class would be only through the makeInstance method.

Singletons pattern in PHP framework, CMS

There are many more examples, don’t hesitate to leave them in the comment below.


In this article, we got an overview about Singletons Pattern. And the most important is how to use it appropriately. Let’s summary about it

  • Singletons are intended to be used when a class must have exactly one instance, no more, no less.
  • With Singleton you absolutely sure about the number of instances, you can change your mind and manage any number of instances.

Related Post

Leave a Reply