/** * 生成数据库唯一实例 * * @param $config * @return object */ public function &instance($config = array()) { static $instances = array(); //实例唯一的编号 $id = serialize($config); if (!isset($instances[$id])) { if (is_string($config)) { $config = $this->parseDNS($config); } if (empty($config['driver'])) { zotop::error(zotop::t('错误的数据库配置文件', $config)); } //数据库驱动程序 $driver = 'database_' . strtolower($config['driver']); //加载驱动程序 if (!zotop::autoload($driver)) { zotop::error(zotop::t('未能找到数据库驱动 "{$driver}"', $config)); } //取得驱动实例 $instance = new $driver($config); //存储实例 $instances[$id] =& $instance; } return $instances[$id]; }
/** * 生成数据库唯一实例 * * @param $config * @return unknown_type */ public function &factory($config = '') { if (is_string($config)) { $config = $this->parseDNS($config); } if (empty($config['driver'])) { zotop::error(-1, 'there is some error in database config'); } $driver = 'Zotop_DataBase_' . ucfirst(strtolower($config['driver'])); if (!zotop::autoload($driver)) { zotop::error(-1, 'the database driver (' . $driver . ') does not support'); } $db = new $driver($config); return $db; }
/** * 类初始化 * * @param string|array config 配置 * @return object */ public function __construct($config = array()) { //支持json格式的缓存配置 if (is_string($config)) { $config = json_decode($config, true); } if (is_array($config)) { $config += array('driver' => zotop::config('system.cache.driver'), 'expire' => (int) zotop::config('system.cache.expire')); } if (empty($config['driver'])) { $config['driver'] = 'file'; } //缓存驱动程序 $driver = 'cache_' . strtolower($config['driver']); //加载驱动程序 if (!zotop::autoload($driver)) { zotop::error(zotop::t('未能找到缓存驱动 "{$driver}"', $config)); } $this->driver = new $driver($config); return $this->driver; }