CodeIgniter 装载类 分析

来源:互联网 发布:淘宝买家修改评论 编辑:程序博客网 时间:2024/04/29 20:14

装载类用于装载元素,这些元素可以是视图文件、辅助函数、模型或者是你自己的文件。(此类由系统自动加载)

一、构造函数:构造函数设置缓冲级别和视图文件、辅助函数等的路径

public function __construct(){$this->_ci_ob_level  = ob_get_level();$this->_ci_library_paths = array(APPPATH, BASEPATH);$this->_ci_helper_paths = array(APPPATH, BASEPATH);$this->_ci_model_paths = array(APPPATH);$this->_ci_view_paths = array(APPPATH.'views/'=> TRUE);log_message('debug', "Loader Class Initialized");}

二、初始化装载类方法:此方法初始化已装载的类、已装载的文件、已装载的模型设置为空数组,并加载_ci_autoloader()私有方法加载autoload.php

public function initialize(){$this->_ci_classes = array();$this->_ci_loaded_files = array();$this->_ci_models = array();$this->_base_classes =& is_loaded();$this->_ci_autoloader();return $this;}
三、library成员方法:调用时用$this->load->library('class_name', $config, 'object_name')。public function library($library = '', $params = NULL, $object_name = NULL):该方法首先判断$library是否为数组,如果为数组使用foreach递归调用library方法。然后判断$library是否为空或者已在$_base_classes中设置,则返回FALSE。然后判断$params如果不为空且不为数组的话,$params则设置为NULL,最后调用$this->_ci_load_class($library, $params, $object_name)加载

public function library($library = '', $params = NULL, $object_name = NULL){if (is_array($library)){foreach ($library as $class){$this->library($class, $params);}return;}if ($library == '' OR isset($this->_base_classes[$library])){return FALSE;}if ( ! is_null($params) && ! is_array($params)){$params = NULL;}$this->_ci_load_class($library, $params, $object_name);}

四、模型加载public function model($model, $name = '', $db_conn = FALSE):首先判断$model是否为数组,如果为数组使用foreach递归调用model方法,然后判断$model是否为空,如果为空则返回FALSE,然后设置$path为空,判断$model是否有子目录,如果有解析出文件名和路径,接着判断自定义对象名是否为空,如果为空设置为$model,接着判断$name是否在$this->_ci_models中,如果在说明已加载,则return;最后加载和实例化model

public function model($model, $name = '', $db_conn = FALSE){if (is_array($model)){foreach ($model as $babe){$this->model($babe);}return;}if ($model == ''){return;}$path = '';// Is the model in a sub-folder? If so, parse out the filename and path.if (($last_slash = strrpos($model, '/')) !== FALSE){// The path is in front of the last slash$path = substr($model, 0, $last_slash + 1);// And the model name behind it$model = substr($model, $last_slash + 1);}if ($name == ''){$name = $model;}if (in_array($name, $this->_ci_models, TRUE)){return;}$CI =& get_instance();if (isset($CI->$name)){show_error('The model name you are loading is the name of a resource that is already being used: '.$name);}$model = strtolower($model);foreach ($this->_ci_model_paths as $mod_path){if ( ! file_exists($mod_path.'models/'.$path.$model.'.php')){continue;}if ($db_conn !== FALSE AND ! class_exists('CI_DB')){if ($db_conn === TRUE){$db_conn = '';}$CI->load->database($db_conn, FALSE, TRUE);}if ( ! class_exists('CI_Model')){load_class('Model', 'core');}require_once($mod_path.'models/'.$path.$model.'.php');$model = ucfirst($model);$CI->$name = new $model();$this->_ci_models[] = $name;return;}// couldn't find the modelshow_error('Unable to locate the model you have specified: '.$model);}


五、装载视图函数:

public function view($view, $vars = array(), $return = FALSE){return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));}

六、装载文件函数:

public function file($path, $return = FALSE){return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));}

七、装载辅助函数:

未完待续。。。