PHP Singleton

Singleton Design Pattern for PHP5.3

I would like to share a very simple Singleton class with you. I've created this long time ago, but since I decided to continue blogging again, I will just copy paste this with some explanation to make a start at blogging again:

First take a look at the code, then read the explanation that follows.

<?php

/**
 * Very simple Singleton object
 *
 * @author Dino Hensen
 *
 */
abstract class Singleton
{

	/**
	 * Member variable holding an Singleton instance
	 *
	 * @var Singleton
	 */
	protected static $_instance = null;

	/**
	 * Returns new or existing instance of Singleton.
	 */
	final public static function getInstance()
	{
		if(null === self::$_instance) {
			self::$_instance = new static();
		}

		return self::$_instance;
	}

	/**
	 * Prevents external object creation
	 */
	final private function __construct() { }

	/**
	 * Prevents object cloning
	 */
	final private function __clone() { }

}

?>

Singleton code Explanation

A class for which you want only one instance to exist at all times is very common in software systems. It is called a Singleton! A Singleton design pattern ensures that it is never possible to create more than one instance of a class that extends the Singleton class defined below.

By using a private constructor you ensure that no other class will be able to create an instance of Singleton class. By adding final to all functions you ensure that no extending class will be able to override this functionality and thus hack your Singleton class.

The getInstance function works as follows: the first time the function is called a Singleton object is instantiated for which protected static $_instance holds a reference to this object. The next time you call getInstance the instance already exists so the reference to the instance is returned. Remember, PHP5 does not pass objects by-value anymore, but instead passes by-reference.

Notice

The Singleton code relies on using PHP5.3 because of something called late static binding.

self::$_instance = new static();

new static() in PHP 5.3's late static binding refers to whatever class in the hierarchy which you call the method on. We need this because PHP needs to know which object to instantiate, that is a 'Singleton' or 'Boss' object. You can read more about late static bindings at: http://www.php.net/manual/en/language.oop5.late-static-bindings.php

Usage

And this is how you use it:

<?php

require_once "singleton.class.php";

class Boss extends Singleton {

	/**
	 * Returns a bossy text
	 * @return string
	 */
	public function whoIsTheBoss()
	{
		return "I am the boss!";
	}

}

$boss = Boss::getInstance();
echo $boss->whoIsTheBoss();	//I am the boss!

?>

You can create a new class which extends Singleton and give it some functionality. Then you can create the object using the public static getInstance function which returns a reference to a Boss object. You now safe the reference in the variable named $boss. When running the code it shows you who's the boss!