// * BASEPATH/system/core/Common. php // Benchmark, Hooks, and Config in the boot file are all loaded using 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 be loaded 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 loaded class. the is_loaded () function is shown below Is_loaded ($ class ); $ _ Classes [$ class] = new $ name (); Return $ _ classes [$ class]; } // Record the loaded classes. The 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; // Initialize all class objects in the boot file (CodeIgniter. php) (steps 4, 5, 6, 7, 8, 9 ), // Register as a member variable of the controller class so that the controller becomes a super object) Foreach (is_loaded () as $ var => $ class) { $ This-> $ var = & load_class ($ class ); } // Load the Loader object and use the Loader object to load a series of resources in 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. Public static function & get_instance () { Return self: $ instance; } } // * BASEPATH/system/core/CodeIgniter. php // Load the base controller class Require BASEPATH. 'core/Controller. php '; // Obtain the controller instance through this global function and obtain this super object, // This function is called 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 automatically uses 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 make 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 //..... // Call the requested function // The segment except class/function in uri will also be passed to the called 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 // Let's look at an example of loading a model for the Loader class. 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 you are loading is The name of a resource that is already being used: '. $ name ); } $ Model = strtolower ($ model ); // Match the path of the model class in sequence. if the path is found, load 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 ); // Register the model object as a member variable of the controller class. This is also true when Loader loads 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. it is called to read the value of an undefined variable. // The following is an implementation of the _ get () function of the Model base class, you can read its variables just like directly in the controller class (for example, $ this-> var ). Function _ get ($ key) { $ CI = & get_instance (); Return $ CI-> $ key; |