


Detailed analysis of thinkPHP5.0 framework automatic loading mechanism
This article mainly introduces the thinkPHP5.0 frameworkautomatic loadingmechanism, and analyzes the concept, principle, usage and related notes of thinkPHP5.0 automatic loading in more detail. Friends in need can refer to
The examples in this article describe the automatic loading mechanism of the thinkPHP5.0 framework. Share it with everyone for your reference, the details are as follows:
Overview
ThinkPHP5.0 truly realizes on-demand loading, and all class libraries use automatic Loading mechanism, and supports class library mapping and automatic loading of composer class libraries.
The implementation of automatic loading is completed by the think\Loader class library, and the automatic loading specification complies with PHP's PSR-4.
Automatic loading
Since the new version of ThinkPHP fully adopts the namespace feature, you only need to correctly define the namespace where the class library is located. If the path of the namespace is consistent with the directory of the class library file, then automatic loading of classes can be achieved.
The automatic loading detection sequence of class libraries is as follows:
1. Class library mapping detection;
2. PSR-4 automatic loading detection;
3. PSR-0 automatic loading Detection;
The system will detect in sequence. Once the detection takes effect, the corresponding class library file will be automatically loaded.
Class library mapping
If we follow our namespace definition specifications above, the automatic loading of the class library can basically be completed, but if more names are defined If there is less space, the efficiency will decrease, so we can define class library mappings for commonly used class libraries. Named class library mapping is equivalent to defining an alias for the class file, which is more efficient than namespace positioning, for example:
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 import class library mapping definitions in batches, for example:
$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 mapping are not required to correspond to namespace directories, 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 function of the system, or do not use a namespace, you can also use the import method of the think\Loader class. Manually load class library files, for example:
Loader::import('org.util.array'); Loader::import('@.util.upload');
Example
// 引入 extends/qrcode.php Loader::import('qrcode', EXTEND_PATH); // 助手函数 import('qrcode', EXTEND_PATH); // 引入 extends/wechat-sdk/wechat.class.php Loader::import('wechat-sdk.wechat', EXTEND_PATH, '.class.php'); // 助手函数 import('wechat-sdk.wechat', EXTEND_PATH, '.class.php');
Class library import also uses a similar concept of namespace (but does not require actual namespace support), supported "root namespace" "include:
Contents | Description |
---|---|
System | Behavior Class Library |
Core Base Class Library | |
traits | System Traits Class Library|
Application Class Library | |
represents the current Module class library package |
The above is the detailed content of Detailed analysis of thinkPHP5.0 framework automatic loading mechanism. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



CSS reflow and repaint are very important concepts in web page performance optimization. When developing web pages, understanding how these two concepts work can help us improve the response speed and user experience of the web page. This article will delve into the mechanics of CSS reflow and repaint, and provide specific code examples. 1. What is CSS reflow? When the visibility, size or position of elements in the DOM structure changes, the browser needs to recalculate and apply CSS styles and then re-layout

As the PHP language becomes more and more popular, developers need to use more and more classes and functions. When a project grows in size, manually introducing all dependencies becomes impractical. At this time, an automatic loading mechanism is needed to simplify the code development and maintenance process. The auto-loading mechanism is a feature of the PHP language that can automatically load required classes and interfaces at runtime and reduce manual class file introduction. In this way, programmers can focus on developing code and reduce errors and time waste caused by tedious manual class introduction. In PHP, generally

Title: An in-depth exploration of the storage location and mechanism of Golang variables. As the application of Go language (Golang) gradually increases in the fields of cloud computing, big data and artificial intelligence, it is particularly important to have an in-depth understanding of the storage location and mechanism of Golang variables. In this article, we will discuss in detail the memory allocation, storage location and related mechanisms of variables in Golang. Through specific code examples, it helps readers better understand how Golang variables are stored and managed in memory. 1.Memory of Golang variables

Go language (also known as Golang) is an efficient programming language developed by Google with features such as concurrency and garbage collection mechanism. This article will explain in detail the garbage collection mechanism in Go language, including its principles, implementation methods and code examples. 1. Principle of garbage collection The garbage collection mechanism of Go language is implemented through the "mark-clear" algorithm. During the running of the program, the Go runtime will track which objects in the heap can be accessed (marked), and which objects cannot be accessed, that is, garbage data (need to be cleared)

Analysis of the implicit conversion mechanism in PHP In PHP programming, implicit conversion refers to the process by which PHP automatically converts one data type to another data type without explicitly specifying type conversion. The implicit conversion mechanism is very common in programming, but it can also easily cause some unexpected bugs. Therefore, understanding the principles and rules of the implicit conversion mechanism is very important for writing robust PHP code. 1. Implicit conversion between integer and floating point types In PHP, implicit conversion between integer and floating point types is very common. When an integer

Popularization of knowledge: Understand the five important concepts of the JS caching mechanism. Specific code examples are required. In front-end development, the JavaScript (JS) caching mechanism is a very critical concept. Understanding and correctly applying caching mechanisms can greatly improve the loading speed and performance of web pages. This article will introduce five important concepts of JS caching mechanism and provide corresponding code examples. 1. Browser cache Browser cache means that when you visit a web page for the first time, the browser will save the relevant resources of the web page (such as JS files, CSS files, pictures, etc.)

How to use PHP7's namespace and automatic loading mechanism to organize the structure of the code? Abstract: With the launch of PHP7, namespace and automatic loading mechanism have become important features that cannot be ignored in PHP development. This article will introduce how to use PHP7's namespace and automatic loading mechanism to organize the structure of the code, and illustrate it through specific code examples. 1. What is a namespace? Namespace is a mechanism introduced in PHP7 to resolve naming conflicts that may occur between different class libraries or code files. via namespace

How can JavaScript achieve automatic scaling of content when scrolling to the bottom of the page and maintain the aspect ratio effect? In modern web design, scrolling to the bottom of the page to automatically load more content has become a common feature requirement. When the loaded content contains images, we often want these images to maintain their original aspect ratio. This article will introduce how to use JavaScript to implement this function and provide corresponding code examples for reference. First, we need to get the scroll position of the page. inJavaScr
