This article brings you the relevant knowledge about the automatic loading of loader in thinkphp. It mainly includes the relevant knowledge about Composer loading. I hope it will be helpful to everyone!
##1. Automatic loading loader source code analysis
1-1 Learning objectives
1-2 Composer loading
Insert image description hereFirst load the loader class in base.php, and then call Register this method. Come to thinkphp\library\think\Loader.php and there is a register method. In this method, we first learn the first knowledge point spl_autoload_register() and talk about it. spl_autoload_register past and present life and simple use, just click to view. The next step is the root path of the project and the path of composer. Insert image description hereStarting from here is loading the composer file, the process is also very simple
1-3 Register namespace
The file is still the register method of thinkphp\library\think\Loader.phpTwo command spaces are registered here, namely think and traits. Then you will enter the addNamespace method
In the addNamespace method, the Psr4 space is added
Then you will come to the addPsr4 method. Both namespaces are registered to the $prefixLengthsPsr4 and $prefixDirsPsr4 properties of the ComposerStaticInit1e269472f484e157e90227b420ffca7a class. In order to verify the above, make a breakpoint debugging , it should be clear after seeing these data. As for traits, the registration method is the same. As of now, the namespace has been registered. Next, let’s study what the psr4 namespace is.1-4 What is Psr4
psr is simply understood as the file path and the relevant specifications for automatically loading the corresponding class , Currently TP5.1 uses the psr4 specificationThe class here refers to the class, interface, and super class structureA complete class requires the following structure\The underscore in any part of the complete class name has no special meaning;
The complete class name can be composed of any uppercase and lowercase letters ;
All class names must be case-sensitive.
The following is an official example. If you can understand this psr specification, try to understand it
1- 5 Load the class library mapping file
At this point, there will definitely be a question, why is there no classmap.php file here?
Don’t rush, don’t panic, first execute php think optimize:autoload to get the file out
You will eventually get there The addClassMap method, in this method, just assigns the data of the classmap.php file to $classMap, there is no other usage
1-6 Automatically load the extend directory
extend This directory is used by everyone who has used the TP framework. Customized class library files can be stored in this directory.
As you can see from the picture below, you use the addAutoLoadDir method to load.
In the method, you only assign the extend path to $fallbackDirsPsr4. this property.
This is the end of the Loader::register(); part. Then we will take an in-depth look at the internal implementation and practical cases.
There are four attributes in the above reading source code, let’s briefly summarize them
2. Briefly explain the loading process of the class
Insert picture description here
Just started parsing the source code here There is a function spl_autoload_register
When the class to be used has not been introduced, this function will be triggered before PHP reports an error. The undefined class name will be passed in as a parameter and think will be executed directly. \\Loader::autoload this method
The first unloaded class after the breakpoint is think\Error
Why is think\Error! You can go back to thinkphp/base.php and take a look. When the automatic loading is completed, the first class to be executed is Error
. You can simply do a test. This Error is changed to Kaka, print it, and the class is changed to Kaka. At this point everyone has a certain understanding of the automatic loading mechanism of this class.
When the class used is not introduced, this class will be passed as a parameter to the autoload method of thinkphp/library/think/Loader.php.
Let’s take a look at the autoload method here
Let’s start with the findFile method and remove the unused The class is passed into this method. In the findFile method, the file mapped by the think\Error class will be directly returned from the classMap attribute.
Return think\ After the full path of the Error class is returned to the file variable of autoload, the size of the win environment is judged once.
Then just use include to introduce the file until it returns.
Up to here is a complete automatic loading and analysis of the class.
Although it ends here, I still have to mention the attribute $classMap. This attribute is based on the file classmap.php. The generation of this file is also required. Generated by executing the command php think optimize:autoload.
How does the program execute when this file is not generated!
All the previous processes are the same, only findFile is different. Let’s briefly sort it out.
The code will definitely not go through classMap at this time
Get the think\Error file first
Then obtain the namespace through the two attributes in Composer's automatic loading, and splice the think\Error.php files
The final result returned is also D:\phpstudy_pro\ WWW\ThinkPHPSourceCodeAnalysis\thinkphp\library\think\Error.php file.
The code here needs to be read carefully.
The automatic loading of classes is completely over here.
3. How to implement automatic loading of classes with custom files
First create a folder kaka
At this time, introduce the file Kaka.php into the controller index
for direct access. At this time, this class will definitely report an error, so what should we do? Just click and you can access it directly!
Insert picture description here
At this time, the importance of the source code is revealed. Remember that in the automatically loaded register function, extend is loaded Directory
Insert picture description here
At this time, add a kaka directory and access it directly
Nothing wrong, it came out directly. Everything is OK. Let’s talk about the loading method of extent.
When I talked about registering the automatic loading class library directory, I just explained that I just saved the path to the $fallbackDirsPsr4 attribute. There is no To elaborate, the next step is to explain these.
The only way to read the source code is to implement it and then view it
Insert picture description here
As long as the defined class is included, it will be included autoload performs automatic loading
It will also enter the findFile method
You can see this code in the findFile method. Is this attribute very Familiar, it is added to the $fallbackDirsPsr4 attribute when the extend directory is automatically loaded.
When printing the parameter class in findFile, look at the data
You can clearly see the test\Kaka class
At this time, print the file returned in the $fallbackDirsPsr4 attribute
Then use __include_file To directly includeD:\phpstudy_pro\WWW\ThinkPHPSourceCodeAnalysis\kaka\test\Kaka.php the file we defined.
How does the above custom file realize automatic loading of classes, and it is also the loading method of extend
(recommended learning: thinkphp5)
The above is the detailed content of How to understand ThinkPHP's Loader automatic loading. For more information, please follow other related articles on the PHP Chinese website!