PHPMVC framework-loader learning Notes

Source: Internet
Author: User
This article will introduce you to the PHPMVC framework loader learning notes. if you need to know more, please refer. the previous section describes the MVC routing principle. in this section, we will talk about the MVC framework loader, also known as the loader. although different frameworks have different loading methods... this article will introduce you to the php mvc framework loader learning notes. For more information, see. the previous section describes the MVC routing principle. in this section, we will talk about the MVC framework loader, also known as the loader. although the use of loaders varies with different frameworks, the principles are the same... all files are loaded in Singleton mode, and the loaded files are cached to avoid repeated loading... in the process of business development, we often need to load different modules and different class libraries. MVC loader can help us implement the Singleton mode and better meet the development requirements. next we will start to use its principle to create a simple loader.

First, the loader we designed must be globally usable, and can be used either in the controller or in the model or library.

Second, the loader must have the cache capability, that is, the same file is loaded only once, and the same class is instantiated only once.

Finally, you must be able to load a variety of files, such as model, library, and helper.

The first step is to define a global function. in this way, you can call this global function in different class libraries and functions, to load the file.

Step 2 and step 3 should be solved together and define a loader class in Singleton mode. because Singleton mode ensures that there is only one copy of content globally. in this case, you can use arrays to cache the loaded content and objects, so as to avoid the problem of repeated loading...

The instance code is as follows:

 $type($name, $data);    }}?>

The above is a simple global function load that I wrote. This function is used to call the loader class globally, the loader class is responsible for loading, instantiating, and caching objects... below I will show you the loader code.

The instance code is as follows:

 _ Loaded_models [$ model]) {return $ this-> _ loaded_models [$ model];} // otherwise, load the file include ROOT. "/application/models/{$ model }. php "; $ class = ucfirst ($ model); // instantiate the object if (emptyempty ($ data) {$ instance = new $ class ;} else {$ instance = new $ class ($ data);} // cache the object $ this-> _ loaded_models [$ model] = $ instance; return $ instance ;} public function library ($ library, $ data = array () {$ library = strtol Ower ($ library); // returns an object if it has been loaded to avoid repeated loading if (isset ($ this-> _ loaded_librarys [$ library]) {return $ this-> _ loaded_librarys [$ library];} // otherwise, load the file include ROOT. "/application/librarys/{$ library }. php "; $ class = ucfirst ($ library); // instantiate the object if (emptyempty ($ data) {$ instance = new $ class ;} else {$ instance = new $ class ($ data);} // cache the object $ this-> _ loaded_librarys [$ library] = $ instance; return $ instanc E ;}}?>

As you can see, this loader adopts the Singleton mode. Although the previous function design can ensure that the loader object is not repeatedly instantiated, however, to ensure that there is only one loader object globally, designing a singleton mode is the safest...

For specific test examples, see the code... I packed the code and uploaded it. if you need it, download it and check it... after downloading, decompress the package, put the lesson2 folder under your www directory, and access yourdomain/MVC/lesson2/index. php/index/test...


Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.