*basepath/system/core/common.php The benchmark,hooks,config in the boot file is loaded by this function. function &load_class ($class, $directory = ' libraries ', $prefix = ' ci_ ') { Record the Loaded class Static $_classes = Array (); has been loaded, read directly and return if (Isset ($_classes[$class])) { return $_classes[$class]; } $name = FALSE; Find the class to load in the specified directory foreach (Array (APPPATH, basepath) as $path) { if (File_exists ($path. $directory. ' /'. $class. '. php ')) { $name = $prefix. $class; if (class_exists ($name) = = = FALSE) { Require ($path. $directory. ' /'. $class. '. php '); } Break } } Not found if ($name = = = FALSE) { Exit (' Unable to locate the specified class: '. $class. php '); } Trace the class that was just loaded, the is_loaded () function below Is_loaded ($class); $_classes[$class] = new $name (); return $_classes[$class]; } Record classes that have already been loaded. function returns all loaded classes function &is_loaded ($class = ") { Static $_is_loaded = Array (); if ($class! = ") { $_is_loaded[strtolower ($class)] = $class; } return $_is_loaded; } *basepath/system/core/controller.php Class Ci_controller { private static $instance; Public Function __construct () { Self:: $instance =& $this; Codeigniter.php all class objects initialized in the boot file (that is, the steps just 4,5,6,7,8,9), Registering as a member variable of the Controller class makes the controller a Super object foreach (is_loaded () as $var = = $class) { $this $var =& Load_class ($class); } //Load the loader object, and then use the loader object to load a series of resources within the program $this->load =& load_class (' Loader ', ' core '); $this->load->initialize (); Log_message (' Debug ', ' Controller Class Initialized '); } This function provides a single instance of the controller externally public static function &get_instance () { Return self:: $instance; } } *basepath/system/core/codeigniter.php Load the base Controller class Require basepath. ' core/controller.php '; With this global function, we get the example of the controller and get the Super object, means to call this function elsewhere in the program to gain control of the entire framework. function &get_instance () { return Ci_controller::get_instance (); } Load the corresponding controller class Note: The router class will automatically use Router->_validate_request () to verify the controller path if (! file_exists (APPPATH. ' controllers/'). $RTR->fetch_directory (). $RTR->fetch_class (). php ')) { Show_error (' Unable to load your default controller. Please do sure the controller specified in your routes.php file is valid. '); } Include (APPPATH. ' controllers/'. $RTR->fetch_directory (). $RTR->fetch_class (). php '); $class = $RTR->fetch_class (); Controller class Name $method = $RTR->fetch_method (); Action Name //..... Calling the requested function Segments other than class/function in the URI are also passed to the calling function Call_user_func_array (Array (& $CI, $method), Array_slice ($URI->rsegments, 2)); Output the final content to the browser if ($EXT->_call_hook (' display_override ') = = = = FALSE) { $OUT->_display (); } *basepath/system/core/loader.php See an example of a loader class loading model. Only part of the code is listed here. Public function model ($model, $name = ", $db _conn = FALSE) { $CI =& get_instance (); if (Isset ($CI-$name)) { Show_error (' The model name loading is the ' The name of the ' a ' resource, ' is already being used: '. $name); } $model = Strtolower ($model); Match the path of the model class in turn, and if found, loads the 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); The model object is still registered as a member variable of the Controller class. Loader will do the same when loading other resources. $CI $name = new $model (); $this->_ci_models[] = $name; Return } Couldn ' t find the Model Show_error (' Unable to locate, the model you have specified: '. $model); } *basepath/system/core/model.php __get () is a magic method that is called when a value of an undefined variable is read The following is an implementation of the model base class for the __get () function, so that in the model class, you can read its variables as if it were directly inside the controller class (for example, $this->var) function __get ($key) { $CI =& get_instance (); return $CI $key; |