: This article mainly introduces an example of the file autoload following the PSR-4, for PHP tutorials interested in students can refer. I. INTRODUCTION
First of all, we need to understand this section. the abbreviation of Proposing a Standards Recommendation (propose standard recommendations) is a PHP development specification, which makes the code we developed more reasonable, better maintained, and more readable. The following standards are available for the DSRs:
- PSR-0: auto load
- PSR-1: basic code specification
- PSR-2: code style
- PSR-3: log interface
- PSR-4: path issues for specification auto-loading
Here we can see that the subscript of the DSRs also starts from 0, and there is also a point like ~ in the array ~. In fact, PSR-4 and PSR-0 is a bit similar or even redundant, they all indicate that the specification of automatic loading, but the specification in the PSR-4 is more concise, in a PSR-0, the underline "_" has a special meaning, and the underline needs to be converted to a directory separator during autoload processing, while the underline does not have any special meaning in the PSR-4, therefore, it is more concise and clearer when files are automatically loaded.
I have made a rough translation of the example in the psr-4 specification above github (I believe your English level must be better than me, certainly can understand ^_^ ), then I made a small example using this automatically loaded class library. The example file is too many and long. it is not suitable here. so I will introduce this example in my blog, for details, go to my github homepage to see this example.
II. Introduction to automatic loading of class libraries
First, let's take a look at the approximate content of the automatically loaded class:
Class Autoload {// register the automatic loading function to the spl autoload stack. public function register (); // add a directory to a namespace prefix. public function addNamespace ($ prefix, $ base_dir, $ prepend = false); // automatically loads a function, the public function loadClass ($ class) will be used in $ this-> register; // find the ing file public function loadMappedFile ($ prefix, $ relative_class ); // check whether a file contains the public function requireFile ($ file) in the file system );}
These functions are used to automatically load class library functions. The register (), addNamespace (), loadMappedFile (), and requireFile () functions are simple and easy to understand at a glance, the only function that may need to be explained is the loadClass function. First, let's look at the code of the loadClass () function:
1 public function loadClass ($ class) 2 {3 // The current namespace prefix 4 $ prefix = $ class; 5 6 // use the namespace to find the corresponding file 7 while (false! ==$ Pos = strrpos ($ prefix, '\') {8 9 // possible namespace prefix 10 $ prefix = substr ($ class, 0, $ pos + 1); 11 12 // the remaining part is a possible class 13 $ relative_class = substr ($ class, $ pos + 1 ); 14 15 // try to load files corresponding to prefix and relitive class 16 $ mapped_file = $ this-> loadMappedFile ($ prefix, $ relative_class); 17 if ($ mapped_file) {18 return $ mapped_file; 19} 20 21 // Move the namespace and relative class to the next location 22 $ prefix = rtrim ($ prefix ,'\\'); 23} 24 25 // the file to be loaded is not found 26 return false; 27}
In fact, there may be only one doubt, that is why we need to recyclically try to find the file. in the while loop, it will gradually shorten the namespace prefix name to find the appropriate namespace prefix. why?
To include more content in the namespace, you do not need to add a new namespace prefix every time you create a folder in the parent namespace, as described in the following figure:
When a file is in a subdirectory of a namespace, you do not need to create a new namespace prefix to successfully load the required file, and the array with a prefix to maintain the namespace has less content, better maintenance. On the contrary, if there is no cyclic search, it is like the following:
Every time you create a new sub-directory, you need to add a new namespace prefix. it is not very troublesome, but it also has some advantages, that is, when loading, you do not dizzy and search for files cyclically, it may reduce the time consumption, but it is a little troublesome during loading.
Therefore, it is more convenient to use the loop loading method, but the directory level without the namespace prefix cannot be too deep, which will consume unnecessary time to file loading. When the efficiency is high, and our directory is certainly not uncertain, in this case, the circular search is removed when loading, but the namespace is added for each directory, which may improve the efficiency, it's just my ignorance.
III. example
Here, you may have a better understanding of the automatically loaded content. at this time, let's take a look at the small examples I have prepared. here we will only introduce the directory structure of the small example, because it is relatively simple, the detailed content will not be listed here. if you are interested, you can go to my github homepage to see this example.
-- Core
-Autoload. php
-- Vendor
-- Test1
-Hello. php
-- Test2
-World. php
-App. php
The copyright of this article belongs to the author (luluyrt@163.com) and the blog Park, without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.
The above introduces an example of the file autoload that follows the PSR-4, including the content of the aspect, hope to be interested in PHP Tutorial friends help.