Rumah  >  Artikel  >  pangkalan data  >  CodeIgniter底层数据库类继承关系

CodeIgniter底层数据库类继承关系

WBOY
WBOYasal
2016-06-07 14:59:501037semak imbas

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类

    <span>require_once</span>(BASEPATH.'database/DB_driver.php'<span>);

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

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

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

<span>$active_group</span> = 'default'<span>;
</span><span>$active_record</span> = <span>TRUE</span><span>;

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


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

 

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

<span>    require_once</span>(BASEPATH.'database/DB_driver.php'<span>);

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

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

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

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

    </span><span>return</span> <span>$DB</span>;

 

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn