选择语言 :

 Module_Database::__construct

new Database('default');

支持 new Database('mysqli://root:123456@127.0.0.1/myqee/'); 的方式

void Module_Database::__construct( [ string $config_name = null ] )

参数列表

参数 类型 描述 默认值
$config_name string 默认值为 Database::DEFAULT_CONFIG_NAME null
返回值
  • void
File: ./modules/database/database.class.php
public function __construct($config_name = null)
{
    if (null===$config_name)
    {
        $config_name = Database::DEFAULT_CONFIG_NAME;
    }

    if (is_array($config_name))
    {
        $this->config = $config_name;
    }
    elseif (false!==strpos($config_name,'://'))
    {
        list($type) = explode('://', $config_name);

        $this->config = array
        (
            'type'         => $type,
            'connection'   => $config_name,
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => false,
            'profiling'    => true,
        );
    }
    else
    {
        $this->config = Core::config('database.' . $config_name);
    }

    $this->config['charset'] = strtoupper($this->config['charset']);

    if (!isset($this->config['auto_change_charset']))
    {
        $this->config['auto_change_charset'] = false;
    }
    if ( $this->config['auto_change_charset'] )
    {
        if ( isset($this->config['data_charset']) )
        {
            $this->config['data_charset'] = strtoupper($this->config['data_charset']);
        }
        else
        {
            $this->config['data_charset'] = $this->config['charset'];
        }
    }

    $driver = $this->config['type'];
    if (!$driver)
    {
        $driver = 'MySQL';
    }
    $driver = 'Database_Driver_' . $driver;

    if (!class_exists($driver, true))
    {
        throw new Exception('Database Driver:' . $driver . ' not found.');
    }

    if (!isset($this->config['connection']))
    {
        throw new Exception('Database connection not set.');
    }

    if (is_string($this->config['connection']))
    {
        $this->config['connection'] = Database::parse_dsn($this->config['connection']);
    }

    # 当前驱动
    $this->driver = new $driver($this->config);

    parent::__construct();

    # 增加自动关闭连接列队
    Core::add_close_connect_class('Database');
}