This code cannot be understood. who can explain it? 100 points?
/*** Model instantiation portal ** @ param string $ model_name model name * @ return obj returned result */function Model ($ model = null) {static $ _ cache = array (); if (! Is_null ($ model) & isset ($ _ cache [$ model]) return $ _ cache [$ model]; $ file_name = BASE_DATA_PATH. '/model /'. $ model. '. model. php '; $ class_name = $ model. 'model'; if (! File_exists ($ file_name) {return $ _ cache [$ model] = new Model ($ model);} else {require_once ($ file_name); if (! Class_exists ($ class_name) {$ error = 'model Error: Class'. $ class_name. 'is not exists! '; Throw_exception ($ error);} else {return $ _ cache [$ model] = new $ class_name ();}}}
Reply to discussion (solution)
/*** Model instantiation portal ** @ param string $ model_name model name * @ return obj returned result */function Model ($ model = null) {// The model name of the array that stores the instantiated model is key and instantiated as value static $ _ cache = array (); // if the cache storage exists, the corresponding storage instance if (! Is_null ($ model) & isset ($ _ cache [$ model]) return $ _ cache [$ model]; // the file path model stored by the model must follow xxxxx. model. php $ file_name = BASE_DATA_PATH. '/model /'. $ model. '. model. php '; // the model type must be xxxxModel $ class_name = $ model. 'model'; if (! File_exists ($ file_name) {// if the file does not exist (the class does not exist) instantiate a Model object return $ _ cache [$ model] = new Model ($ model );} else {// if the file exists (the class exists), it will contain require_once ($ file_name); // if (! Class_exists ($ class_name) {$ error = 'model Error: Class'. $ class_name. 'is not exists! '; Throw_exception ($ error);} if else {// exists, the instance is put in the cache and return $ _ cache [$ model] = new $ class_name ();}}}
In fact, it is the instantiation of the Model and an object cache, which is usually implemented using autoload.
This code is used to return the $ model instance class.
There are three return methods:
1. if the instance has already passed the static variable, it will be returned directly;
2. if a class file exists in the model Directory, instantiate the class
3. if the file does not exist, an instance of this type is directly returned.