think\Db::connect PHP Метод

connect() публичный статический Метод

数据库初始化 并取得数据库类实例
public static connect ( mixed $config = [], boolean | string $name = false ) : Connection
$config mixed 连接配置
$name boolean | string 连接标识 true 强制重新连接
Результат Think\Db\Connection
    public static function connect($config = [], $name = false)
    {
        if (false === $name) {
            $name = md5(serialize($config));
        }
        if (true === $name || !isset(self::$instance[$name])) {
            // 解析连接参数 支持数组和字符串
            $options = self::parseConfig($config);
            if (empty($options['type'])) {
                throw new \InvalidArgumentException('Underfined db type');
            }
            $class = false !== strpos($options['type'], '\\') ? $options['type'] : '\\think\\db\\connector\\' . ucwords($options['type']);
            // 记录初始化信息
            if (App::$debug) {
                Log::record('[ DB ] INIT ' . $options['type'], 'info');
            }
            if (true === $name) {
                return new $class($options);
            } else {
                self::$instance[$name] = new $class($options);
            }
        }
        return self::$instance[$name];
    }

Usage Example

Пример #1
0
 /**
  * 架构函数
  * @param array $options 缓存参数
  * @access public
  */
 public function __construct($options = [])
 {
     if (!empty($options)) {
         $this->options = array_merge($this->options, $options);
     }
     $this->handler = \think\Db::connect(!empty($this->options['hostname']) || !empty($this->options['dsn']) ? $this->options : []);
 }
All Usage Examples Of think\Db::connect