CodeIgniter底层数据库类继承关系

php中文网
发布: 2016-06-07 14:59:50
原创
1251人浏览过

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>;    
登录后复制

 

360 AI助手
360 AI助手

360公司推出的AI聊天机器人聚合平台,集合了国内15家顶尖的AI大模型。

360 AI助手 140
查看详情 360 AI助手

如果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>;
登录后复制

 

 

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号