Automatic loading mechanism such as tp5.1 source code snooping


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.

Automatic loading mechanism of classes in tp5.1

First enter the project entry file

If you install ittp5.1.39,composer create-project topthink/think=5.1.* tp5


//1. Define a namespace

get into base.php File, see what operations have been done

Automatic loading mechanism such as tp5.1 source code snooping

1. IntroductionLoaderClass, it should be noted that only the introduction of the class, and did not execute anything.

Automatic loading mechanism such as tp5.1 source code snooping

2. ImplementationLoader::register()Method to register the automatic loading mechanism

Automatic loading mechanism such as tp5.1 source code snooping

  • Let’s go into the method and use it firstspl_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 calledLoader::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 theautoload_static.phpFile, and then get the declared class file, and pop up the last class name, which isautoload_static.phpFile, and assign the properties of this class to the properties of the loader class. This step can be understood as registrationapp,think\composerNamespace and corresponding directory

  • Then register againthink,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 directoryextend

3. Then executeError::register()method

  • becausethink\ErrorClass is not introduced in advance, so the system calls the system auto loading mechanism registered in the previous step to executeLoader::autoloadMethod, and the parameter isthink\Error, enterautoloadMethod, first check whether there is such a name in the class library alias, and if there is, use theclass_aliasFunctions are aliased and loaded automatically. If there is no class library, then search the file according to the class name, that is, executefindFileMethod, 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

  • Then executeregisterMethod to register exception handling

4. Register class library alias,Loader::addClassAliasFor autoload



1. Due toautoload_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 namespaceLoader::findFileMethod string capture splicing class file location usefullength

4. What would you do if you needed to add a new namespace or an autoload directory?

It’s mainly about how to use itLoad::addNamespaceandLoader::addAutoLoadDir, inLoaderInternal and external can, but in order not to modify the source code, it is recommended to call add in the entry file

5. Library aliasclass_alias

Eg. direct useConfigClass, the automatic loading mechanism will directly callLoader::$classAliasThe key value class library of the corresponding key name in the property

namespace app\index\controller;

This work adoptsCC agreementReprint must indicate the author and the link of this article

Give each hair a name before it’s all gone~