在编写 CodeIgniter 应用程序时,我的控制器操作往往以如下几行开头:
$this->load->model('abc_model');
$this->load->library('ijk');
然后(只是为了完整性)它们的使用如下:
$this->abc_model->fetch_123();
$this->ijk->do_something();
延长会不会有什么问题MY_Controller
那么以下是可能的吗?
$this->model('zbc_model')->fetch_stuff();
$this->library('ijk')->do_something();
Pros:
- 类在实际使用时才会加载
- 不需要使用自动加载任何类
config/autoload.php
- 稍微干净的代码(可以说)
Cons:
- 每次访问都会调用额外的方法(不过通常只是返回已加载的实例)
- 代码稍微混乱(可以说)
使用菲尔·斯特金的技术 http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY,将其添加到您的 application/config/config.php
/*
| -------------------------------------------------------------------
| Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)