缘由
app/controllers/Index.php中有如下代码
public function disable(){
$this->yredis->set('name','tb');
var_dump($this->yredis->get('name'));
$this->load->view('welcome_message');
}
发现这个yredis没有load,怎么来的?翻翻手册,有自动加载配置
在app/config/autoload.php中配置,部分内容如下
| -------------------------------------------------------------------
| Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in system/libraries/ or your
| application/libraries/ directory, with the addition of the
| 'database' library, which is somewhat of a special case.
|
| Prototype:
|
| $autoload['libraries'] = array('database', 'email', 'session');
|
| You can also supply an alternative library name to be assigned
| in the controller:
|
| $autoload['libraries'] = array('user_agent' => 'ua');
*/
$autoload['libraries'] = array('Yredis','validation'=>'sn');
追踪一下
那么他这个自动加载是在代码内如何实现的呢?肯定从头$CI大对象来看。
在system/core/Controller.php中,有如下两句
$this->load =& load_class('Loader', 'core');
$this->load->initialize();
通过common.php中定义的load_class方法,加载了loader类。不得不说ci的核心基本都在这里了。(这个load还是个未定义的属性么...?)
我们去看system/core/Loader.php中的initialize方法,不过load的时候肯定是先加载__construct方法
public function __construct()
{
// (ob机制,不要乱配~)
$this->_ci_ob_level = ob_get_level();
$this->_ci_classes =& is_loaded();
log_message('info', 'Loader Class Initialized');
}
再回来看当前文件的initialize方法:
public function initialize()
{
$this->_ci_autoloader();
}
再去看_ci_autoloader...
protected function _ci_autoloader()
{
if (file_exists(APPPATH.'config/autoload.php'))
{
include(APPPATH.'config/autoload.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');
}
if ( ! isset($autoload))
{
return;
}
// Autoload packages
if (isset($autoload['packages']))
{
foreach ($autoload['packages'] as $package_path)
{
$this->add_package_path($package_path);
}
}
// Load any custom config file
if (count($autoload['config']) > 0)
{
foreach ($autoload['config'] as $val)
{
$this->config($val);
}
}
// Autoload helpers and languages
foreach (array('helper', 'languag