TP5 Code Learning Note: Loader

Source: Internet
Author: User
Tags autoload composer install

Auto Load Loader class A. Overview

The system calls the Loader::register () method to register for automatic loading, and after this is done, all conforming class libraries, including the third-party class library that the Composer relies on for loading, are automatically loaded.
The automatic loading of the system consists of two parts:

1. Automatic loading method of registration system \think\loader::autoload

2. Registration composer automatic loading (conforms to composer specification)

The automatic load detection sequence for a class library is:

1. Whether to define class library mappings;

2.Composer automatic load detection;

3. Whether it is a registered namespace;

4. Detect the Extended class library under the Extend_path directory;

If any of the above is detected, it is automatically loaded according to the PSR-4 naming convention.

As you can see, the way to define class library mappings is most efficient.

two. AutoLoad class

In loader::autoload ($class); Print out in $class , run as follows:

The visible $class is the class name with the namespace.

To detect namespace aliases: is there automatic loading ?

For example, a class kukiiu/uploadis defined. Give him a namespace alias orgso I can use the org/upload class to redefine the kukiiu/upload class with a org/upload class.

To define a namespace alias method:

1.\think\loader::addnamespacealias (' org ', ' kukiiu ');

check whether class library mappings are defined

Print out $map to see each element inside it as follows:

["Think\app"]=> string "F:\hwphp\study\thinkphp\library\think\App.php"

Once the mapping is defined, when the class is directly used, it is searched for the file pathname given by the definition, and then the file is loaded.

A number of commonly used class mappings are defined in mode/common.php.

To define a mapping method:

1.\think\loader::addmap (' Test ', App_path. Index\controller\test.php ');

Dump (class_exists (' Test '));

is 2.mode/common.php alias added to the map ?

Composer automatic loading

after the composer install is executed, the dependent files are created under Thinkphp/vendor.

1.TP automatic loading mechanism under parsing vendor/composer at initialization Loader::registercomposerloader ().

A) load the 'composer/autoload_namespaces.php ' file of the PSR-0 specification.

After parsing, get self:: $PREFIXESPSR 0 is:

b) Load the ' composer/autoload_psr4.php ' file of the PSR-4 specification.

After parsing, we get $PREFIXLENGTHSPSR 4 as:

$PREFIXDIRSPSR 4 is:

c) Load class map file (same as TP 's class map):' composer/autoload_classmap.php '.

d) Load the class file directly ... ' Composer/autoload_files.php '

2. when looking for a class, use Loader::findfileincomposer () to see if it is a class under composer.

It's a little complicated, look again later!

namespaces are loaded automatically

The class-map is not set, basically it is loaded by the class here. Print out the class that goes into the namespace to load automatically to see:

Resolving Namespaces

List ($name, $class) = explode (' \ \ ', $class, 2);

Take the first \ Previous value of the $class :

such as "App\index\controller\index" get $name =' app ' $class = ' index\controller\index '

if (Isset (self:: $namespace [$name])) {

Registered Namespaces

$path = self:: $namespace [$name];

} elseif (Is_dir (Extend_path. $name)) {

extending the class library namespace

$path = Extend_path. $name. DS;

} else {

return false;

}

$filename = $path. Str_replace (' \ \ ', DS, $class). EXT;

1. register the namespace:

Judging $name values are not in the registered $namespace , where the namespace and concept namespaces are not the same meaning, easy to confuse, registered here $namespace is an identity, feel called namespace prefix will be better, according to PSR-0 specification should be called ' Supplier vendor', see mode/ common.php can see that some of the default $namespace Definitions:

The $namespace printed in loader::auotload () is as follows:

So $namespace of the namespace: Span style= "Font-family:calibri" >\ $class as a prefix, this prefix to find the path it represents. such as app "F:\hwphp\study\ application\ "This path, after which if you are using a class under this path, such as a namespace of \app\myclass\test f:\hwphp\study\application\myclass\ test.php

For example: in TP frame, each module is placed in application $namespace There is a ' app ' that represents this path, so the class in the module is as long as the namespace conforms to TP \app\index\controller\index Controller, it can be converted to "F:\hwphp\study\application\index\controller\Index.php" file path.

the namespace auto-load class for TP is based on the PSR-4 standard, defined $namespace key is the namespace prefix, Value is the base path of the class. After you get the full name of the class: replace the namespace prefix with the base path, add the rest of the namespaces to it, and add the class name and suffix. unlike PSR-4 , where the namespace prefix is only one layer, eliminating the loop-judging prefix operation (list ($name, $class) = explode (' \ \ ', $class, 2);). The simple process is as follows:


To manually register a namespace (flag) method:

1. Add the following code to the application portal file:

\think\loader::addnamespace (' Kukiiu ', ' ... /extend/kukiiu/');

\think\loader::addnamespace ([

' Kukiiu ' = ' ... /extend/' Kukiiu '/',

' org ' and ' = '. /extend/org/',

]);

2. Add the configuration to the app's profile and the system will automatically register when the app executes.

' Root_namespace ' = [

' My ' = ' ... /application/extend/my/',

]

3. under mode/common.php $namespace under increase.

2. extending the Class library namespace:

Thinkphp implements the automatic registration namespace (identity) mechanism,

Put your own class library package directory into the Extend_path directory (thinkphp/extend, configurable), you can automatically register the corresponding namespace (identity),

For example: Add a mylib directory below the Thinkphp/extend directory and define a \mylib\test class ( the class file is located in the thinkphp/extend/mylib/ test.php), you can call new \mylib\test () directly;

The Extension class Library directory can be customized by defining constant Extend_path in the portal file.

As can be seen, the extension of TP class is based on the PSR-0 automatic loading standard.

  Other functions:

...


Resources:

1.HTTP://WWW.PHP-FIG.ORG/PSR. PSR Standard website.



TP5 Code Learning Note: Loader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.