Concrete analysis of thinkPHP5.0 framework automatic loading mechanism

Source: Internet
Author: User
Tags traits
This article mainly introduces the thinkPHP5.0 framework automatic loading mechanism, the detailed analysis of thinkPHP5.0 automatic loading concept, principle, usage and related considerations, the need for friends can refer to the following

This paper describes the automatic loading mechanism of thinkPHP5.0 framework. Share to everyone for your reference, as follows:

Overview

ThinkPHP5.0 really implements on-demand loading, all class libraries take the automatic loading mechanism, and support class library mapping and automatic loading of composer class libraries.

The implementation of the automatic loading is done by the Think\loader class library, and the automatic loading specification conforms to PHP's PSR-4.

Auto Load

Because the new version of thinkphp completely takes on the namespace, it is only necessary to properly define the namespace where the class library is located, and the path to the namespace is consistent with the directory of the class library file, so you can implement the automatic loading of the class.

The automatic load detection sequence for class libraries is as follows:

1, class library mapping detection;
2, PSR-4 automatic load detection;
3, PSR-0 automatic load detection;

The system will be detected sequentially and the corresponding class library file will be automatically loaded once the test is in effect.

Class Library Mappings

Following our namespace definition specification above, we can basically complete the automatic loading of the class library, but if we define more namespaces, the efficiency will decrease, so we can define class library mappings for the common class libraries. Naming a class library mapping is equivalent to defining an alias for a class file that is more efficient than a namespace location, such as:

Loader::addclassmap (' Think\log ', Lib_path. ' think\log.php '); Loader::addclassmap (' Org\util\array ', Lib_path. ' org\util\array.php ');

You can also use the Addclassmap method to bulk Import class library mapping definitions, such as:

$map = [  ' think\log ' +   lib_path. ' think\log.php ',  ' org\util\array ' + lib_path. ' Org\util\ Array.php ']; Loader::addclassmap ($MAP);

Although classes registered through class library mappings can not enforce the requirement for the corresponding namespace directory, it is still recommended to follow the PSR-4 specification to define class libraries and directories.

Class Library Import

If you do not need the automatic loading of the system, or if you do not use a namespace, you can also use the import method of the Think\loader class to manually load the class library file, for example:

Loader::import (' Org.util.array '); Loader::import (' @.util.upload ');

Example

Introduction of Extends/qrcode.phploader::import (' QRCode ', extend_path);//Helper function Import (' QRCode ', extend_path);//Introduction extends/ Wechat-sdk/wechat.class.phploader::import (' Wechat-sdk.wechat ', Extend_path, '. class.php ');//Helper function Import (' Wechat-sdk.wechat ', Extend_path, '. class.php ');

Class Library Imports also take the concept of similar namespaces (but do not require actual namespace support), and the supported "root namespaces" include:

directory description
behavior system behavior Class library
think core base Class library
traits system traits class library
app application class Library
@ Represents the current Module class library package


If you fully comply with the system's namespace definition, you generally do not need to manually load the class library file, directly instantiated.

Composer Automatic loading

Version 5.0 supports automatic loading of composer-installed class libraries, and you can directly call them directly in the namespace of the composer Dependent Library.

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.