Follow PSR-4 Auto-loadFirst, Introduction
First of all, here to understand psr,proposing a standards recommendation (the standard recommendation) abbreviation, is a PHP development specification, let us develop the code more reasonable, better maintenance, more readable. There are several criteria for PSR:
- PSR-0: Auto Load
- PSR-1: Basic code specification
- PSR-2: Code Style
- PSR-3: Log interface
- PSR-4: Routing problem for spec auto-loading
Here we see that the subscript for PSR is also starting from 0, and the array is a bit like ~. In fact, PSR-4 and PSR-0 are somewhat similar or even redundant, they are all explained is the automatic loading specification, but PSR-4 in the specification is more concise, in the PSR-0 underlined "_" is a special meaning, in the autoload processing need to convert the underscore to the directory separator, And in the PSR-4 underline is not any special meaning, so in the file automatically loaded when the more concise, more clear conditioning.
I have made a general translation of the examples in the PSR-4 specification above GitHub (I believe your English level must be better than me, you can certainly read ^_^), and then use this automatic loading class library to do a small example, the example file many, long, put here is not suitable, So I'm going to introduce this example in my blog, and I'd like to know more about this example from my GitHub homepage.
second, automatic loading class library introduction
First look at the approximate contents of the auto-load class:
ClassAutoload {//Register the auto-load function into the SPL autoload stack.PublicfunctionRegister ();//Add a table of contents to a namespace prefixPublicfunction AddNamespace ($prefix,$base _dir,$prepend =false); // auto-load function, $this->register public function loadclass ( $class ); // look for the mapped file public function loadmappedfile ( $prefix, $relative _class< Span style= "color: #000000;" >); // see if a file exists in the file system function requirefile ( $file ); }
The automatic loading of class library functions in these functions, where register (), AddNamespace (), Loadmappedfile (), Requirefile () functions are relatively simple, one can understand, The only function that may need to be explained is the LoadClass function, which first looks at the code of the LoadClass () function:
1Publicfunction LoadClass ($class)2{3//The current namespace prefix4$prefix =$class;56//To find the corresponding file by namespace7while (False!==$pos =Strrpos ($prefix, ' \ \ ')) {89//Namespace prefixes that may exist10$prefix =Substr$class, 0,$pos + 1);1112//The remainder is a class that may exist13$relative _class =Substr$class,$pos + 1);1415//Attempting to load the prefix prefix and relitive class corresponding file16$mapped _file =$this->loadmappedfile ($prefix,$relative _class);17if ($mapped _file) {18Return$mapped _file19 }20 21 // move namespace and relative class split position to next position 22 $prefix = rtrim ($ prefix, ' \ \ ' ); 23 }24 25 // The file that was trying to load was not found 26 Span style= "color: #0000ff;" >return false;
In fact, there may be a doubt that there is only one place, that is why the loop to try to find the file, in the while loop, will slowly shorten the name of the namespace prefix to find the appropriate namespace prefix, why do you want to do so?
The loop looks for a file to contain more content in the namespace, without having to add a new namespace prefix each time you create a new folder in the parent namespace, as described in the following figure:
When a file is under a subdirectory in a namespace, we do not have to create a new namespace prefix to successfully load the required files, and maintain namespace prefixes with fewer arrays and better maintenance. Conversely, if there is no loop lookup, this is the way it looks.
Each time a new subdirectory will go to a new namespace prefix, is not very troublesome, but there is a certain advantage, that is, when loading the loop to find files, may reduce a certain amount of time consumption, but it is a bit cumbersome to load.
Therefore, it is more convenient to load this method in a loop, but it is important to not let the directory hierarchy with no namespace prefixes be too deep, which consumes unnecessary time to file loading. When the need for high efficiency, and our directory is certainly not uncertain, this time when loading to remove the loop lookup, but to add a namespace for each directory, the efficiency may be improved, just a little bit of my humble opinion.
Iii. Examples
Said here you may have been on the automatic loading of the content, this time to see my small example of the preparation, here is just a small example of the directory structure, because the relatively simple, detailed content is no longer listed here, interested Tongxu can go to my GitHub homepage to see this example
+autoload
+core
|_autoload.php
+vendor
+test1
|_hello.php
+test2
|_world.php
|_app.php
This article copyright belongs to the author ([email protected]) and the blog garden in total, without the author's consent to prohibit any form of reprint, reproduced article must be in the article page obvious location to the author and the original text connection, otherwise reserves the right to pursue legal responsibility.
psr-php Development Specification (copyright in this article [email protected])