Due to the impact of the epidemic, xiaozhidian marketing and offline pre-sale were suspended for a period of time. It was originally recommended to use the wechat live broadcast function. However, due to the leadership’s strong recommendation to use the third-party tool xiaoegong, the project was postponed and only the online business was developed and maintained. Then learn something new while you have time. However, it is not important to learn what new knowledge, the most important thing is not to learn. I’m also very confused. I don’t know what to learn first. I always want to learn this and that. However, in the end, I still stay in the same place and learn from the bitter experience. I’ll make a plan first. It’s stipulated that I will learn this within a period of time, and the rest will be ignored. Let’s talk about it after one. Before the interview, people often ask, have you studied the source code of a framework. Let’s start with the source code of the research framework. As the recent project is developed with tp5.1.39, let’s study tp5.1.
First enter the project entry file
If you install it
composer create-project topthink/think=5.1.* tp5
//1. Define a namespace
LoaderClass, it should be noted that only the introduction of the class, and did not execute anything.
Loader::register()Method to register the automatic loading mechanism
Let’s go into the method and use it first
spl_autoload_registerThe automatic loading mechanism of the registration system means that when the system creates a new object and finds that the class does not exist, it is called
Loader::autoloadStatic method to load the corresponding class
Then get the root directory of the project, set the directory of the composer of the project, and import the
autoload_static.phpFile, and then get the declared class file, and pop up the last class name, which is
autoload_static.phpFile, and assign the properties of this class to the properties of the loader class. This step can be understood as registration
think\composerNamespace and corresponding directory
Then register again
traitsNamespace and corresponding directory
Then register the class name mapping,
runtime/classmap.php, which is generated by post optimization,
php think optimize:autoload
Then register the autoload directory
3. Then execute
think\ErrorClass is not introduced in advance, so the system calls the system auto loading mechanism registered in the previous step to execute
Loader::autoloadMethod, and the parameter is
autoloadMethod, first check whether there is such a name in the class library alias, and if there is, use the
class_aliasFunctions are aliased and loaded automatically. If there is no class library, then search the file according to the class name, that is, execute
findFileMethod, first check whether there is a class map, If it exists, it will return the file directory address. If it does not exist, it will look up psr-4 and return the file directory address. If it cannot be found, it will continue to look up the psr-4 auto loading directory. If it cannot be found again, it will look up the psr-0 file directory. If it cannot be found again, it will look up the psr-0 auto loading directory, return the file directory address, and finally import the file
registerMethod to register exception handling
4. Register class library alias,
1. Due to
autoload_static.phpThe class name in the file is changing all the time. We can’t get a fixed class name. How can we merge the properties of this class with those of loader?
//There are two main functions to get_ declared_ classes property_ exists
2. Why use namespace mapping?
Save resources, easy to call, avoid every loop search
3. The psr4 standard top-level namespace mapping array uses two arrays. The first one uses the first letter of the namespace as the prefix index, and then the top-level namespace. But it is not the file path, but the length of the top-level namespace. Why?
Because the psr4 standard replaces the top-level namespace with the top-level namespace directory, it is very important to obtain the length of the top-level namespace
Loader::findFileMethod string capture splicing class file location useful
4. What would you do if you needed to add a new namespace or an autoload directory?
It’s mainly about how to use it
LoaderInternal and external can, but in order not to modify the source code, it is recommended to call add in the entry file
5. Library alias
Eg. direct use
ConfigClass, the automatic loading mechanism will directly call
Loader::$classAliasThe key value class library of the corresponding key name in the property
This work adoptsCC agreementReprint must indicate the author and the link of this article