Implementing Singleton Base Class



With the very exciting PHP 5.3 nearing release, i thought i’d write about one of the very useful functions making its way in to this version. The new get_called_class() function on its doesn’t really do a lot, but this simple little function plugs one of the many gaping holes in PHPs object model.

Just one of the things this fantastic new function can be used for is a very elegant implementation of singleton base class. We can simply extend this to create a number of fully functioning singletons.

Ok, lets take a look at some code.

The first thing we need to do is create an singleton class, heres one i prepared earlier.

< ?php
class Singleton_Test
{
    //enforce singleton
    final private function __clone() {}
    final private function __construct() {}

    final public static function getInstance()
    {
        static $instance = null;

        if (null === $instance) {
            $instance = new Singleton_Test();
        }
        return $instance;
    }
}

This does exactly what is says on the tin, we get one instance and one instance only. Now lets see what happens under normal circumstances if we try to implement an abstract singleton.

< ?php
abstract class Singleton_Test
{
    //enforce singleton
    final private function __clone() {}
    protected function __construct() {}

    final public static function getInstance($class)
    {
        static $instance = array();

        if (!isset($instance[$class])) {
            $instance[$class] = new $class();
        }
        return $instance[$class];
    }
}

You see the first problem we encounter is the static instance property, we need each singleton child to hold reference to its self. The obvious solution is to hold an array of child classes (lazy load style). Next is the glaring line "$instance = new Singleton_Test();" we need to loose this explicit call and replace it with something relative. An easy but far from elegant solution is to pass the name of the class you are instantiating into the getInstance() method.

Thanks to the new get_called_class() function we can now do this dynamically, resulting in much more elegant code. Here goes.

< ?php
abstract class Singleton_Test
{
    //enforce singleton
    final private function __clone() {}
    protected function __construct() {}

    final public static function getInstance()
    {
        static $instance = array();

        $class = get_called_class();

        if (!isset($instance[$class])) {
            $instance[$class] = new $class();
        }
        return $instance[$class];
    }
}

Fantastic isn't it? Just one extra line and we have an all-singing all-dancing fully extendable singleton base class. :-)

One Response to “Implementing Singleton Base Class”

  1. Derekp says:

    I think i’ve seen this somewhere before…but it’s not bad at all

Leave a Reply




We're Listed in the e internet directory .co.uk Under: Website Design | Web Design Company - UK Web Designers & Developers