CodeIgniter延迟加载库/模型/ etc [英] CodeIgniter lazy-loading libraries/models/etc

查看:169
本文介绍了CodeIgniter延迟加载库/模型/ etc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写CodeIgniter应用程序时,我的控制器操作往往开始于以下几行:

When writing CodeIgniter applications my controller actions tend to begin with a few lines as below:

    $this->load->model('abc_model');
    $this->load->library('ijk');

然后(仅仅为了完整性),它们的用法如下:

And then (just for completeness) they're used as follows:

    $this->abc_model->fetch_123();
    $this->ijk->do_something();

扩展 MY_Controller 以下是可能的?

    $this->model('zbc_model')->fetch_stuff();
    $this->library('ijk')->do_something();

优点:


  1. 类别在实际使用之前不会加载

  2. 不需要使用 config / autoload.php

  3. 稍微干净的代码(可以说)



Cons:


  1. 每次访问都会调用额外的方法(通常只返回已加载的实例)

  2. 可以论证)


推荐答案

使用Phil Sturgeon的技术,将其添加到您的应用程序/ config / config.php

Use Phil Sturgeon's technique, add this to your 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 );
 }
}

这篇关于CodeIgniter延迟加载库/模型/ etc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆