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.
<?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');
?>
<?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];
}
?>