Home  >  Article  >  PHP Framework  >  How to understand ThinkPHP's Loader automatic loading

How to understand ThinkPHP's Loader automatic loading

WBOY
WBOYforward
2021-12-20 14:06:381933browse

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!

How to understand ThinkPHP's Loader automatic loading##1. Automatic loading loader source code analysis

1-1 Learning objectives

  • Automatic loading of classes

  • Two ways of automatic loading of classes

  • spl_autoload_register must use

  • to implement automatic loading of custom file classes

1-2 Composer loading

How to understand ThinkPHPs Loader automatic loading

Insert image description here

First load the loader class in base.php, and then call Register this method.

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

Insert image description here

Starting from here is loading the composer file, the process is also very simple

  • Judge whether composer is a directory

  • Judge whether autoload_static.php under the path is a file

  • Introduce autoload_static.php file

  • Return all declared classes array return

  • Get the last class ComposerStaticInit30742487e00917c888d89ba216f165b9

  • Judge ComposerStaticInit30742487e00917c888d89ba216f165b9 exists Data in the array

How to understand ThinkPHPs Loader automatic loading

Then you can go to the vendor\composer\autoload_static.php file to see these two attributes

How to understand ThinkPHPs Loader automatic loading

There is a piece of code here. It is estimated that some students will go around self::${$attr} = $composerClass::${$attr};. The $attr here is 'prefixLengthsPsr4' , 'prefixDirsPsr4', 'fallbackDirsPsr4', 'prefixesPsr0', 'fallbackDirsPsr0', 'classMap', 'files', these data, add a $ sign in the outer layer.

Thus, the corresponding attribute values ​​​​are directly obtained in the ComposerStaticInit30742487e00917c888d89ba216f165b9 class, which is the two attribute values ​​in the above figure.

How to understand ThinkPHPs Loader automatic loading

1-3 Register namespace

The file is still the register method of thinkphp\library\think\Loader.php

Two command spaces are registered here, namely think and traits. Then you will enter the addNamespace method

In the addNamespace method, the Psr4 space is addedHow to understand ThinkPHPs Loader automatic loading

How to understand ThinkPHPs Loader automatic loading

Then you will come to the addPsr4 method. Both namespaces are registered to the $prefixLengthsPsr4 and $prefixDirsPsr4 properties of the

ComposerStaticInit1e269472f484e157e90227b420ffca7a class.

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

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 specification

The class here refers to the class, interface, and super class structure

A complete class requires the following structure\( \)*\

The following specifications come from PHP documentation

  • The complete class name must have a top-level Namespace, called "vendor namespace";

  • The complete class name can have one or more sub-namespaces;

  • Complete Class names must have a final class name;

  • 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

How to understand ThinkPHPs Loader automatic loading

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?

How to understand ThinkPHPs Loader automatic loading

Don’t rush, don’t panic, first execute php think optimize:autoload to get the file out

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

In the method, you only assign the extend path to $fallbackDirsPsr4. this property.

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

2. Briefly explain the loading process of the class

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

The first unloaded class after the breakpoint is think\Error

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

. 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.

How to understand ThinkPHPs Loader automatic loading

Let’s take a look at the autoload method here

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

Get the think\Error file first

How to understand ThinkPHPs Loader automatic loading

Then obtain the namespace through the two attributes in Composer's automatic loading, and splice the think\Error.php files

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

At this time, introduce the file Kaka.php into the controller index

How to understand ThinkPHPs Loader automatic loading

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!

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

Insert picture description here

At this time, add a kaka directory and access it directly

How to understand ThinkPHPs Loader automatic loading

Nothing wrong, it came out directly. Everything is OK. Let’s talk about the loading method of extent.

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

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

How to understand ThinkPHPs Loader automatic loading

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.

How to understand ThinkPHPs Loader automatic loading

When printing the parameter class in findFile, look at the data

You can clearly see the test\Kaka class

How to understand ThinkPHPs Loader automatic loading

How to understand ThinkPHPs Loader automatic loading

At this time, print the file returned in the $fallbackDirsPsr4 attribute

How to understand ThinkPHPs Loader automatic loading

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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete