All pastes #2008524 Raw Edit

Something

public php v1 · immutable
#2008524 ·published 2010-12-02 12:15 UTC
rendered paste body
<?php/*** */abstract class Singleton{  static private $__object_pool = array();  static protected $__use_args_sig = false;  /**  *   */  private function __construct() {/*...*/}  /**  *   */  private function __clone() {/*...*/}  /**  *   */  protected function __init() {/*...*/}  /**  *   */  static final function get_instance()  {    $args = func_get_args();    $class = get_called_class();    // By default we use class name as a key.    $key = $class;    if (static::__use_args_sig() && count($args)) {      $sig = md5(join($args));      $key .= $sig;    }    if (!isset(self::$__object_pool[$key])) {      $instance = new $class();      call_user_func_array(array($instance, '__init'), $args);      self::$__object_pool[$key] = $instance;      return $instance;    }    else {      return self::$__object_pool[$key];    }  }  /**  *   */  static final function __singleton_add_instance($instance)  {    $class = get_class($instance);    if (array_key_exists($class, self::$__object_pool)) {      return;    }    self::$__object_pool[$class] = $instance;  }  /**  *   */  static protected function __use_args_sig()  {    return false;  }}//// Implementation of DB class.//class DB extends Singleton{  function __init()  {  }}//// How to use//// Connection to default database.$db = DB::get_instance();// Execute query$db->select('select * from `test_tab`);// Send query to another database where $dsn is a database source name.$db2 = DB::get_instance($dsn);$db2->select('select * from `test_tab`);?>