How can I understand the following code? I 'd better explain it at the joint points.
* Behavior model instance ** @ param string $ model name * @ return obj returned result */function Logic ($ model = null, $ base_path = null) {static $ _ cache = array (); $ cache_key = $ model. '. '. $ base_path; if (! Is_null ($ model) & isset ($ _ cache [$ cache_key]) return $ _ cache [$ cache_key]; $ base_path = null? BASE_DATA_PATH: $ base_path; $ file_name = $ base_path. '/logic/'. $ model. '. logic. php'; $ class_name = $ model. 'lock'; if (! File_exists ($ file_name) {return $ _ cache [$ cache_key] = new Model ($ model);} else {require_once ($ file_name); if (! Class_exists ($ class_name) {$ error = 'logic Error: Class'. $ class_name. 'is not exists! '; Throw_exception ($ error);} else {return $ _ cache [$ cache_key] = new $ class_name ();}}}
Reply to discussion (solution)
Function Logic ($ model = null, $ base_path = null) {// defines the static variable static $ _ cache = array (); // defines the cache key value $ cache_key = $ model. '. '. $ base_path; // if there is an instance of this model in a static variable, if (! Is_null ($ model) & isset ($ _ cache [$ cache_key]) return $ _ cache [$ cache_key]; // path of the organization class file $ base_path = null? BASE_DATA_PATH: $ base_path; $ file_name = $ base_path. '/logic /'. $ model. '. logic. php '; $ class_name = $ model. 'logic '; // whether the class file exists if (! File_exists ($ file_name) {// A model return $ _ cache [$ cache_key] = new Model ($ model );} else {// introduces require_once ($ file_name) if (! Class_exists ($ class_name) {// if it does not exist, an exception is thrown. $ error = 'logic Error: Class'. $ class_name. 'is not exists! '; Throw_exception ($ error);} else {// instantiate it if it exists, save to static array and return $ _ cache [$ cache_key] = new $ class_name ();}}}