Controller.php--控制器超级类
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//CodeIgniter控制器超级类
class CI_Controller {
private static $instance;
//构造函数
public function __construct()
{
//当前对象的引用
self::$instance =& $this;
//分配CodeIgniter.php文件中所有被实例化的类对象到当前对象属性中
//因此,CI程序能把这些属性当成超级对象来使用
//意味着我们所写的控制器只要继承CI_Controller,就可以使用所有加载的类(方法、属性)。
foreach (is_loaded() as $var => $class)
{
$this->$var =& load_class($class);
}
$this->load =& load_class('Loader', 'core');
$this->load->initialize();
log_message('debug', "Controller Class Initialized");
}
//获取CodeIgniter控制器示例
public static function &get_instance()
{
return self::$instance;
}
}
CI_Controller的构造函数中添加的属性都是对load_class函数中的静态变量$_class中的元素的引用。
CI_Controller的实现方式应用了单例模式或者称单元素模式,CI_Controller::get_instance()方法返回的是对静态属性instance的引用。