Singleton Pool

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Singleton Pool

Post by McGruff »

Code: Select all

<?php
/*
    Class: SingletonPool

    Store multiple singletons (of different classes, naturally).

*/
class SingletonPool
{
    /*
        param (string) $class - the class name
    */
    function &instance($class)
    {
        static $ob;

        if(!isset($ob[$class]))
        { 
            $ob = array();
            $ob[$class] = new $class;
        }
        $this->ob[$class] =& $ob[$class];
    }

    /*
        param (string) $class - the class name
    */
    function &getInstance($class) 
    {
        return $this->ob[$class];
    }

}
///////////////
// END CLASS //
///////////////


/*
    A utility fn to reduce clutter when fetching something from the pool.
    param (string) $class - the class name
*/
function &singleton_pool($class) 
{
    $ob =& new SingletonPool;
    $ob->instance($class);
    return $ob->getInstance($class);
}

// in use (reference is optional - use if wish to edit the singleton instance):
$myclass =& singleton_pool('MyClass');

?>
Singletons explained: phppatterns.com.

You wouldn't want to over-use singletons. In my current project I found I needed two or three - hence this class.
Last edited by McGruff on Tue Aug 09, 2005 12:39 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Mac, your code won't work with multiple classes because of:

Code: Select all

//...snip
        if(!isset($ob[$class])) 
        { 
            $ob = array();
//...snip
So if I first pass "myClass" to instance method, and then "myClass2" - ob static array will get overwritten. I'd suggest the following:

Code: Select all

function &instance($class) 
    { 
        static $ob=array(); 

        if(!isset($ob[$class])) 
        { 
            $ob[$class] = new $class; 
        } 
        $this->ob[$class] =& $ob[$class]; 
    }
Tested on PHP 4.3.something
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yes - thanks for the tip. Some sharp eyes you have there. It's good to have your input on phpdn.

Next time I'll test properly before posting a snippet ;)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

A class was easier to test when writing the code but, after another look, the same can be achieved with a simple fn:

Code: Select all

<?php
/*
    param (string) $class
    - a class name to instantiate
    - assumes class def has already been included
*/ 
function &singletonPool($class)
{
    static $ob = array();

    if(!isset($ob[$class]))
    { 
        $ob[$class] = new $class;
    }
    return $ob[$class];
}
?>
Post Reply