I have been thinking for a long time and finally want to write a series of articles. I expect to write a series of articles that can be handled for promotion and interview. As you read this article, if you find hot interview questions or technical difficulties that you didn’t write, look forward to pointing them out in the comments section and working on them together.

preface

At present, I am sorting out the PHP progress roadmap, if there is a good suggestion, Kaka will be included in the first time.

One, automatic loading loader source analysis

1-1 Learning objectives

  • Class
  • Class auto-loading in two ways
  • Spl_autoload_register must be used
  • Automatic loading of classes that implement custom files

1-2 Composer loads

Insert the picture description here

From the parse diagram above, the loader class is first loaded in base.php, and then the register method is called.

Came tothinkphp\library\think\Loader.phpThere is a register method, and in this method, let’s learn the first point firstspl_autoload_register()Talk about spl_Autoload_Register and how to use it, directly click to view.

This is followed by the project root path and the composer path.

Insert the picture description here

From here, you are loading the Composer file, and the process is simple

  • 1. Check whether composer is a directory
  • 2. Check whether autoload_static. PHP is a file
  • 3. Import the autoload_static.php file
  • 4. Return all declared class array returns
  • 5. Get the final class ComposerStaticInit30742487e00917c888d89ba216f165b9
  • 6. Determine whether the ComposerStaticInit30742487e00917c888d89ba216f165b9 exist in the array data

Then go to vendor\composer\autoload_static.php to see these two properties

Here’s a little bit of code that some of you might want to go around hereself::${$attr} = $composerClass::${$attr};Here,$attris'prefixLengthsPsr4', 'prefixDirsPsr4', 'fallbackDirsPsr4', 'prefixesPsr0', 'fallbackDirsPsr0', 'classMap', 'files'The data, the outer layer is one$Symbols.

In ComposerStaticInit30742487e00917c888d89ba216f165b9 directly obtain the corresponding attribute values in this class, which is above the two attribute values.

Insert the picture description here

1-3 Registering a namespace

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

Two command Spaces are registered here, called think and traits. And then it’s going to go into this addNamespace methodaddNamespaceMethod, addedPsr4 space

Then you go to the addPsr4 method, which registers both namespacesComposerStaticInit1e269472f484e157e90227b420ffca7a class $$prefixDirsPsr4 prefixLengthsPsr4 and the two attributes

In order to verify the above to do a breakpoint debugging, the data should be clear to see, as fortraitsIt’s the same way to register.

Now that the namespace registration is complete, let’s take a look at what the PSR4 namespace is.

1-4 What the hell is a Psr4

PSR is simply the file path and the related specification of automatically loading the corresponding class. Currently, TP5.1 uses PSR4 specification

Class here refers to class, interface, superclass structure

A complete class requires the following structure \< namespace >(\< subnamespace >)*\< class name >

The following specifications are derived from the PHP documentation

  • The full class name must have a top-level namespace called “vendor namespace”;

  • A full class name can have one or more subnamespaces;

  • The full class name must have a final class name;

  • The slide line in any part of the full class name has no meaning;

  • The full class name can consist of any uppercase and lowercase letters;

  • All class names must be case sensitive.

Here is an example given by the authorities, as much as possible to understand the PSR specification

1-5 Loading the class library mapping file

At this point, one must wonder why there is no classmap.php file.No hurry, no panic, just execute firstphp think optimize:autoloadGet the files outIt’s going to end upaddClassMapThis method, in this method, just takesclassmap.phpThe data in this file is assigned to$classMapIt’s just, what else can it be used for

1-6 Automatically load the extend directory

Extend this directory is used by those who have used the TP framework in some way. This directory can be used to store custom library files.

The addAutoLoadDir method is used to load the file

In the method, we just assign the extend path to$fallbackDirsPsr4This property.

I’m going to stop hereLoader::register();That concludes the section, and let’s take a closer look at the internal implementation and practice cases.

There are four attributes in the above source code, which are simple to sort out

Insert the picture description here

Class loading process

Insert the picture description here

There was a function spl_autoload_register just when you started parsing the source here

If the class is not introduced, this function will be triggered before the PHP error. The undefined class name will be passed as an argument and the method think\\Loader::autoload will be executed directly

The first class not loaded after the breakpoint is think\Error

Why think\Error? You can go back tothinkphp/base.phpLet’s see, the first class to be executed when auto-loading is complete is Error

You can simply test this Error by changing it to Kaka, printing it out and changing the class to Kaka. Here you have a certain understanding of the automatic loading mechanism of this class.

When using the class has not be introduced by the class as a parameter to thinkphp/library/think/Loader. The PHP autoload method.

Let’s take a look at the autoload method

Let’s go from the findFile method, pass the uninitialized class into the method, and the findFile method will return the think\Error file directly from the classMap property

Return the full path of the class think\Error toautoloadthefileAfter variable, the case of the WIN environment is judged once.

Then simply include the file until it returns.

Up to this point is a full auto-load parsing of the class.

Although this is the end, but still have to mention a point is$classMapThis property, this property is based on the fileclassmap.phpThe generation of this file also requires executing commandsphp think optimize:autoloadThe generated.

How does the program execute when this file is not generated?

All the previous processes are the same, except for findFile, which is different. Let’s go through it briefly.

The code is definitely not going to go to the classMap

Get the think\Error file first

Then use the two properties automatically loaded by Composer to obtain the namespace, and then concatenate the think\ error.php file

And so does the resultD:\phpstudy_pro\WWW\ThinkPHPSourceCodeAnalysis\thinkphp\library\think\Error.phpThis file.

The code here needs to be read carefully.

The auto-loading of the class is completely over at this point.

How to realize automatic class loading of custom files

First create a folder kaka

In this case, introduce the kaka.php file in the controller index

This class will definitely report an error, so what should we do to access it directly?

Insert the picture description here

This is where it becomes important to cash out the source code. Remember that the extend directory was loaded in the register function that was automatically loaded

Insert the picture description here

Add a kaka directory and access it directly

There’s nothing wrong with it. It just came out. All OKHere’s how extents are loaded

The directory is stored in the $fallbackDirsPsr4 property. The directory is stored in the $fallbackDirsPsr4 property. The directory is stored in the $fallbackDirsPsr4 property.

Reading the source code is only about implementing that and then looking at that

Insert the picture description here

As long as the class is defined will go into the Autoload for automatic loading

You’ll also go to the findFile method

You can see this code in the findFile method. If this property is familiar, it is added to the extend directory when it is automatically loaded$fallbackDirsPsr4Properties.

Take a look at the data when you print the class parameter in findFile

You can clearly see the test\Kaka class

Now I’m printing this$fallbackDirsPsr4Property

And then use__include_fileTo direct the includeD:\phpstudy_pro\WWW\ThinkPHPSourceCodeAnalysis\kaka\test\Kaka.phpThe file that we define.

How does the above custom file implement the auto-loading of the class, and that is, the loading mode of extend

Four,

The process of auto-loading the class is complete. If there are any errors, you can find them in the comments section.

Insist on learning, insist on blogging and insist on sharing is the belief that Kaka has been holding since its inception. Hope in so big Internet article can bring you a filar silk help. I’m Ka-ka, see you next time.