This code cannot be understood. who can explain it? 100 points?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.