CI为了实现动态继承在DB.php中做了一些特殊处理.在分析代码的时候我找了半天也没有找到CI_DB这个类,后来看了DB这个类后发现里面是动态eval出来实现的继承关系.我靠,真tmd的恶心.
DB.php总就一个DB方法,没有任何类,恶心就恶心在这里,根本没有仔细看然后又去别的地方找这个CI_DB...
看代码:
// Load the DB classes. Note: Since the active record class is optional
// we need to dynamically create a class that extends proper parent class
// based on whether we're using the active record class or not.
// Kudos to Paul for discovering this clever use of eval()
if ($active_record_override !== NULL)
{
$active_record = $active_record_override;
}
require_once(BASEPATH.'database/DB_driver.php');
是否使用active_record?如果使用active_record则创建一个CI_DB继承于 CI_DB_active_record
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 //不适用active_record则创建CI_DB继承CI_DB_driver
{
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();
}
if (isset($params['stricton']) && $params['stricton'] == TRUE)
{
$DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
}
return $DB;