In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "ThinkPHP automatic loading Loader source code analysis and loading class introduction", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "ThinkPHP automatic loading Loader source code analysis and loading class introduction" bar!
1. Automatic loading loader Source Code Analysis 1-1 automatic loading of learning target classes spl_autoload_register must be able to use 1-2 Composer loading of classes that implement custom files
Insert a picture description here
From the parsing diagram given by Kaka above, the loader class is first loaded in base.php, and then the register method is called.
There is a register method when you come to thinkphp\ library\ think\ Loader.php. In this method, we first learn the first knowledge point, spl_autoload_register (), to talk about the past life and simple use of spl_autoload_register, which can be viewed by clicking directly.
This is followed by the root path of the project and the path of composer.
Insert a picture description here
From here on, you are loading the composer file, and the process is very simple.
1. Determine whether composer is directory 2. Determine whether the autoload_static.php under the path is file 3. Introduce autoload_static.php file 4. Returns an array of all classes that have been declared and returns 5. Get the last class ComposerStaticInit30742487e00917c888d89ba216f165b96. Determine whether there is data in the array in ComposerStaticInit30742487e00917c888d89ba216f165b9
Then you can see these two attributes in the vendor\ composer\ autoload_static.php file.
Here is a piece of code that estimates that some students will go around self::$ {$attr} = $composerClass::$ {$attr};, where the $attr is the 'prefixLengthsPsr4',' prefixDirsPsr4', 'fallbackDirsPsr4',' prefixesPsr0', 'fallbackDirsPsr0',' classMap', 'files', with a $sign added to the outer layer.
Thus, the corresponding attribute values are directly obtained in the ComposerStaticInit30742487e00917c888d89ba216f165b9 class, that is, the two attribute values in the figure above.
Insert picture description 1-3 registration namespace here
The file is also the register method of thinkphp\ library\ think\ Loader.php
Two command spaces are registered here, think and traits. And then you go to the addNamespace method, and in the addNamespace method, you add the Psr4 space.
Then come to the addPsr4 method, which registers both namespaces in the $prefixLengthsPsr4 and $prefixDirsPsr4 attributes of the ComposerStaticInit1e269472f484e157e90227b420ffca7a class.
In order to verify the above to do a breakpoint debugging, see these data should be clear, as for traits is the same way to register.
Now that the namespace is registered, let's take a look at what a psr4 namespace is.
What is 1-4 Psr4?
Psr is simply understood as the file path, the relevant specifications of the corresponding classes are loaded automatically, and the current TP5.1 uses the psr4 specification.
The classes here refer to class, interfaces, and superclass structures
A complete class requires a structure\ (\) *\
The following specifications are derived from PHP documents
A complete class name must have a top-level namespace called "vendor namespace"
A complete class name can have one or more subnamespaces
The complete class name must have a final class name
The descending line in any part of the complete class name has no special meaning.
The full class name can be made up of any uppercase and lowercase letters
All class names must be case sensitive.
Here 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 must be a question about why there is no classmap.php file. Don't worry, first execute php think optimize:autoload to get the file out and you will eventually go to the addClassMap method. In this method, you just assign the data from the classmap.php file to $classMap, and there is no other use.
1-6 automatically load the extend directory
Extend this directory has used the TP framework more or less, in this directory can store custom class library files.
According to the following figure, you can see that the addAutoLoadDir method is used for loading.
In the method, you simply assign the path of extend to the attribute $fallbackDirsPsr4.
This ends with Loader::register ();, and then we take an in-depth look at the internal implementation and practice cases.
There are four attributes in the above reading source code, which can be simply sorted out.
Insert a picture description here. 2. briefly describe the loading process of the class. insert a picture description here.
At the beginning of parsing the source code here, there is a function spl_autoload_register.
When the class that needs to be used is not introduced, this function will be triggered before the PHP reports an error, and the undefined class name will be passed as an argument. Here, the method think\\ Loader::autoload will be executed directly.
The first unloaded class after the breakpoint is think\ Error
Why think\ Error! You can take a look at it when you go back to thinkphp/base.php. The first class to execute after the automatic loading is completed is Error.
You can simply do a test, change the Error to Kaka, print it, and then change the class to Kaka. At this point, you will have a certain understanding of the automatic loading mechanism of this class.
When the class used is not introduced, the class is passed as a parameter to the autoload method of thinkphp/library/think/Loader.php.
Come here and take a look at the autoload method.
Let's start with the findFile method and pass the unintroduced classes into this method. In the findFile method, the think\ Error file mapped by the class will be returned directly from the classMap attribute.
After returning the full path of the class think\ Error to the file variable of autoload, the case of the win environment is determined once.
Then you can import the file directly using include until it is returned.
Until this is a complete automatic loading and parsing of the class.
Although it's over here, I still have to mention the attribute of $classMap, which is based on the file classmap.php, and the generation of this file also needs to execute the command php think optimize:autoload.
How does the program execute when this file is not generated?
All the previous processes are the same, except in the case of findFile, which is followed by a simple comb.
The code will definitely not go at this time, classMap.
Get the think\ Error file first
Then the namespace is obtained by two attributes in Composer automatic loading, and the think\ Error.php file is spliced.
The final result is also the file D:\ phpstudy_pro\ WWW\ ThinkPHPSourceCodeAnalysis\ thinkphp\ library\ think\ Error.php.
The code here needs to be read carefully.
The automatic loading of the class here is completely over.
Third, how to realize the automatic loading of classes in custom files
Create a folder kaka first
At this time, the file Kaka.php is introduced into the controller index
Direct access, at this time this class will definitely report an error, so how should we operate, we can directly access it!
Insert a picture description here
This time reflects the importance of the source code, but also remember that in the automatically loaded register function, the extend directory was loaded
Insert a picture description here
At this time, add a kaka directory and visit it directly.
There's nothing wrong with it. It just came out. Everything OK is here to talk about how extent is loaded.
The previous talk about registering the autoload class library directory just explained that it just saved the path to the $fallbackDirsPsr4 attribute, not in detail, and then I'll explain it.
The only way to read the source code is to realize that and then check that.
Insert a picture description here
As long as it is a defined class, it will go into autoload to load automatically.
It will also enter the findFile method.
You can see this code in the findFile method, which is not familiar with, but is added to the $fallbackDirsPsr4 property when the extend directory is automatically loaded.
Take a look at the data when printing the parameter class in findFile
You can clearly see the class test\ Kaka
Print the file returned in the $fallbackDirsPsr4 attribute at this time
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 the automatic loading of classes, and that is how extend loads?
Thank you for reading, the above is the "ThinkPHP automatically load Loader source code analysis and loading class introduction" of the content, after the study of this article, I believe you automatically load ThinkPHP Loader source code analysis and load class introduction of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.