Difficult-php Tutorial

Source: Internet
Author: User
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 ();}}}

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.