Skip to main content.
Last update: 09-25-2005
Contributed by Tony Marston
"In software engineering, a design pattern is a general solution to a common problem in software design. A design pattern isn't a finished design that can be transformed directly into code, it is a description or template for how to solve a problem that can be used in many different situations."
Print Print E-mail Email


The Singleton Design Pattern for PHP

In software engineering, a design pattern is a general solution to a common problem in software design. A design pattern isn't a finished design that can be transformed directly into code, it is a description or template for how to solve a problem that can be used in many different situations.

One of these design patterns is called the Singleton. It's purpose can be described quite briefly as follows:

Ensure a class has only one instance and provide a global point of access to it.

It achieves this by only creating a new instance the first time it is referenced, and thereafter it simply returns the handle to the existing instance.

Why should you want this facility in the processing of a web page? There are several reasons:

As with all design patterns there is more than one way in which it can be implemented, so in the following sections I will document some of the methods that I have encountered.

You should also be aware that the code samples in this document will work in both PHP 4 and PHP 5.

A non-class Helper function

This method uses a separate non-class function as a "helper". It contains code similar to the following:

function &getDateObject ()
// return the handle to the standard date validation object.
{
static $instance;

if (!is_object($instance)) {
// does not currently exist, so create it
require_once 'std.datevalidation.class.inc';
$instance = new DateClass;
} // if

return $instance;

} // getDateObject

It is referenced with code similar to the following:

    $dateobj =& getDateObject();

This method has the following advantages:

This method has the following disadvantages:

A separate Helper method within each class

This method requires the addition of a getInstance() (or similar) method within each each and every class, similar to the following:

    function &getInstance ()
// this implements the 'singleton' design pattern.
{
static $instance;

if (!isset($instance)) {
$c = __CLASS__;
$instance = new $c;
} // if

return $instance;

} // getInstance

It is referenced with code similar to the following:

    require_once 'std.datevalidation.class.inc';
$dateobj =& DateClass::getInstance();

This method has the following advantages:

This method has the following disadvantages:

A single Helper method for all classes

This requires the creation of a separate class (called singleton in this example) with a single method (called getInstance() in this example).

class singleton
{
function &getInstance ($class)
// implements the 'singleton' design pattern.
{
static $instances = array(); // array of instance names

if (!array_key_exists($class, $instances)) {
// instance does not exist, so create it
$instances[$class] = new $class;
} // if

$instance =& $instances[$class];

return $instance;

} // getInstance

} // singleton

It is referenced with code similar to the following:

    require_once 'std.datevalidation.class.inc';
$dateobj =& singleton::getInstance('DateClass');

This method has the following advantages:

This method has the following disadvantages:

References

Some of the terms used in this document may be new to you, so here are some explanations:

Conclusion

As you can see there is no single way to implement this design pattern, and this is the most simple pattern there is! Just think of the possible variations for the more complex patterns.


Tony Marston
26th July 2005

http://www.tonymarston.net