参考了DevNet 和Chinaunix.net 的讨论,用以下代码简单实现单子模式(SINGLETON): 代码片段:
/**
* Class: SingletonPool
* Store multiple singletons (of different classes, naturally).
*
*/
class Singleton {
/**
* 返回唯一对象,可保存多个对象
*
* @param (string) $class - the class name
* @return object
* @access public
* @static
*/
function &getInstance($class) {
static $ob = array();
if( !isset($ob[$class]) ) {
$ob[$class] = & new $class;
}
return $ob[$class];
}
}
?>
http://www.ooso.net