欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

CodeIgniter底层数据库类继承关系

程序员文章站 2022-06-11 18:29:35
...

1、CI_DB_mysql_driver 继承CI_DB, CI_DB这个类是不存在的,每次调用文件中~/CodeIgniter_2.1.3/system/database/DB.php函数DB来初始化database的时候, 根据用户是否开启active_record,生成继承CI_DB_active_record或CI_DB_driver类的CI_DB类, 2、CI_DB_a

1、CI_DB_mysql_driver 继承 CI_DB, CI_DB这个类是不存在的,每次调用文件中~/CodeIgniter_2.1.3/system/database/DB.php函数&DB来初始化database的时候,

根据用户是否开启active_record,生成继承CI_DB_active_record或CI_DB_driver类的CI_DB类,

2、CI_DB_active_record类继承CI_DB_driver类

    require_once(BASEPATH.'database/DB_driver.php');

    if ( ! isset($active_record) OR $active_record == TRUE)
    {
        require_once(BASEPATH.'database/DB_active_rec.php');

        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }
    }
    else
    {
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }

3、最终会调用*父类CI_DB_driver的构造方法,将数据库配置的各字段赋值到CI_DB_driver类的各个字段上,

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = ''; 
$db['default']['password'] = ''; 
$db['default']['database'] = ''; 
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = ''; 
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = ''; 
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = ''; 
$db['default']['autoinit'] = TRUE;


        var $username;
    var $password;
    var $hostname;
    var $database;
    var $dbdriver        = 'mysql';
    var $dbprefix        = '';
    var $char_set        = 'utf8';
    var $dbcollat        = 'utf8_general_ci';
    var $autoinit        = TRUE;
    var $port            = '';
    var $pconnect        = FALSE;
    var $db_debug        = FALSE;    

如果autoinit自动初始化为TRUE,则将DB初始化,这个DB对象是CI_DB_mysql_driver类

    require_once(BASEPATH.'database/DB_driver.php');

    if ( ! isset($active_record) OR $active_record == TRUE)
    {
        require_once(BASEPATH.'database/DB_active_rec.php');

        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }
    }
    else
    {
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }

    require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');

    // Instantiate the DB adapter
    $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    $DB = new $driver($params);
    if ($DB->autoinit == TRUE)
    {
        $DB->initialize();
    }

    return $DB;