This article will introduce you to the PHPMVC framework loader learning notes. if you need to know about it, you can refer to the MVC routing principle mentioned in the previous section. in this section, we will talk about the MVC framework loader, also known
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:
- If (! Function_exists ('load '))
- {
- Function load ($ name, $ type = 'model', $ data = array ())
- {
- Static $ loader = NULL;
- If (is_null ($ loader ))
- {
- Include (ROOT. '/system/core/loader. php ');
- $ Loader = Loader: get_instance ();
- }
- Return $ loader-> $ 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:
-
- Class Loader
- {
- Private $ _ loaded_models = array ();
- Private $ _ loaded_librarys = array ();
- Private static $ OBJ;
- # Singleton mode. new objects are not allowed.
- Private function _ construct ()
- {
- }
- Public static function get_instance ()
- {
- If (is_null (self: $ OBJ ))
- {
- $ Class = _ CLASS __;
- Self: $ OBJ = new $ class;
- }
- Return self: $ OBJ;
- }
- Public function model ($ model, $ data = array ())
- {
- $ Model = strtolower ($ model );
- # If the object has been loaded, the system returns an object to avoid repeated loading.
- If (isset ($ this-> _ loaded_models [$ model])
- {
- Return $ this-> _ loaded_models [$ model];
- }
- # Otherwise, load the file
- Include ROOT. "/application/models/{$ model}. php ";
- $ Class = ucfirst ($ model );
- # Instantiate an object
- If (emptyempty ($ data ))
- {
- $ Instance = new $ class;
- }
- Else
- {
- $ Instance = new $ class ($ data );
- }
- # Cache objects
- $ This-> _ loaded_models [$ model] = $ instance;
- Return $ instance;
- }
- Public function library ($ library, $ data = array ())
- {
- $ Library = strtolower ($ library );
- # If the object has been loaded, the system returns an object 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 an object
- If (emptyempty ($ data ))
- {
- $ Instance = new $ class;
- }
- Else
- {
- $ Instance = new $ class ($ data );
- }
- # Cache objects
- $ This-> _ loaded_librarys [$ library] = $ instance;
- Return $ instance;
- }
- }
- ?>
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...