Yii2 in-depth learning--automatic loading mechanism, YII2 deep learning--
YII2 automatic loading is divided into two parts, part is the automatic loading mechanism of Composer, and the other part is the automatic loading mechanism of YII2 framework itself.
Composer Automatic loading
For the library's auto-load information, Composer generates a vendor/autoload.php
file. You can simply introduce this file and you will get support for an auto-load.
In the previous article, the introduction of the entry file, we can see the following:
// introducing the autoload.php file in vendor, the class is automatically loaded based on the composer mechanism require (__dir__. '/.. /vendor/autoload.php ');
Because this series is mainly about Yii2, so the Composer automatic loading mechanism is not detailed here.
Access to Information:
Automatic loading mechanism of YII2 frame
The automatic loading of the YII2 framework is implemented by the Spl_autoload_register method.
In the previous article, the introduction of the entry file, we can see the following:
// file yii.php with YII framework introduced require (__dir__. '/.. /vendor/yiisoft/yii2/yii.php ');
What is the content of yii.php? How is automatic loading implemented?
Let's take a look at the following yii.php:
Php/** * yii bootstrap file. * * @link http://www.yiiframework.com/* @copyright Copyright (c) yii software LLC * @lic Ense http://www.yiiframework.com/license/*/require(__dir__. '/baseyii.php ');/** * Yii is a helper class serving common framework functionalities. * * It extends from [[\yii\baseyii]] which provide s the actual implementation. * By writing your own Yii class, you can customize some functionalities of [[\yii\baseyii]]. * * @author Qiang Xue
* @since 2.0
*/classYiiextends\yii\baseyii{}/** * spl_autoload_register-Registers a given function as an implementation of __autoload * * BOOL Spl_autoload_register ([Callable $autoload _function [, BOOL $throw = True [, bool $prepend = false]]]) * * Registers the function with the SPL __autoload function queue. If the functions in the queue have not been activated, they are activated. * If the __autoload () function has been implemented in your program, it must be explicitly registered in the __autoload () queue. * Because the Spl_autoload_register () function replaces the __autoload () function in Zend Engine with spl_autoload () or Spl_autoload_call (). * If more than one autoload function is required, spl_autoload_register () satisfies such requirements. * It actually creates a queue of autoload functions, executed one by one, in the order they were defined. * In contrast, __autoload () can only be defined once. * * Autoload_function * To register the automatic loading function. If no arguments are provided, the default implementation function Spl_autoload () for AutoLoad is automatically registered. * * Throw * This parameter sets whether Spl_autoload_register () throws an exception when Autoload_function fails to register successfully. * * prepend * if True,spl_autoload_register () adds a function to the top of the queue instead of the tail of the queue. * Yii registered Yii autoload function, implement automatic loading, in fact, now \yii\baseyii*/Spl_autoload_register ([' Yii ', ' autoload '],true,true);//Map defining the class name and file address of the YII coreYII::$classMap=require(__dir__. '/classes.php ');//Create a Dependency injection container for YiiYII::$container=NewYii\di\container ();
The main content is to introduce the baseyii.php file, then declare the class Yii, inherit the Baseyii, and then register the YII (actually Baseyii) AutoLoad method, to achieve automatic loading. After that, the Yii core class name and the file address one by one map are introduced, and stored in Yii:: $classMap. Finally, a Yii\di\container instance is created and stored in the Yii:: $container.
You can see that the key code for automatic loading is:
true true);
Let's take a look at the implementation of the AutoLoad method in Baseyii, which reads as follows:
/** * Class autoload loader. * This method was invoked automatically when PHP sees a unknown class. * The method would attempt to include the class file according to the following procedure: * * 1. Search in [[Classmap]]; * 2. If the class is namespaced (e.g. ' yii\base\component '), it'll attempt * to include the file associated with the C Orresponding Path alias * (e.g. ' @yii/base/component.php '); * * This autoloader allows loading classes, follow the [PSR-4 Standard] (http://www.php-fig.org/psr/psr-4/) * A ND has its top-level namespace or sub-namespaces defined as path aliases. * * example:when aliases ' @yii ' and ' @yii/bootstrap ' is defined, classes in the ' yii\bootstrap ' namespace * would be loaded using the ' @yii/bootstrap ' alias which points to the directory where bootstrap extension * files is instal LED and all classes from other ' Yii ' namespaces'll be loaded from the YII framework directory. * * AlsoThe [Guide sections on autoloading] (guide:concept-autoloading). * * @param string $className The fully qualified class name without a leading backslash "\" * @throws Unknownclass Exception if the class does not exist in the class file*/ Public Static functionAutoLoad$className) { //Auto Load Class if(isset(Static::$classMap[$className])) { //if the class exists in the $CLASSMAP, use it directly $classFile=Static::$classMap[$className]; //if the first string is ' @ ', it means that the corresponding file address is an alias, which translates it into a real file address if($classFile[0] = = = ' @ ') { $classFile=Static:: Getalias ($classFile); } } ElseIf(Strpos($className, '\\') !==false) { //if ' \ \ ' is present, it means that it contains namespace, can be spelled as an alias, and then gets the real file address according to the alias . $classFile=Static:: Getalias (' @ ').Str_replace('\\', '/',$className) . '. php ',false); //did not get the true file address or the address obtained is not a file, it returns an empty if($classFile===false|| !Is_file($classFile)) { return; } } Else { return; } //introduce files of this class include($classFile); //if it is debug mode and $className is not a class, not an interface, or a trait, throw an exception if(Yii_debug &&!)class_exists($className,false) &&!interface_exists($className,false) &&!trait_exists ($className,false)) { Throw NewUnknownclassexception ("Unable to find"$className' In file:$classFile. Namespace missing? "); } }
Among them, you may not be very clear Getalias method, this method is actually the YII2 in the alias into a real file address, about the method of the specific content, will be explained in detail later.
Give some examples to help you understand.
If Yii:: $classMap The values are as follows:
Yii::$classMap = [ ' app/test/test ' = '/var/www/basic/webtest/test.php '];
When you use the ' App/test/test ' class, the '/var/www/basic/webtest/test.php ' file is automatically introduced, and the content of the project is certainly not this way, which is just a simple example for everyone to understand.
In the example above, if you use the ' Yii\base\component ' class, it will be converted to the ' @yii/base/component.php ' Alias and then introduced in the file address that gets its name based on the alias.
The above is the basic content of YII2 automatic loading mechanism ~ ~
YII2 source interested students can pay attention to the project yii2-2.0.3-annotated, now on the above has been added a lot of comments on Yii2 source code, and then continue to add ~
Interested students can also participate in the submission of YII2 source comments.
http://www.bkjia.com/PHPjc/1063519.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063519.html techarticle Yii2 in-depth study--automatic loading mechanism, YII2 deep learning--YII2 's automatic loading is divided into two parts, part is the Composer automatic loading mechanism, the other part is the YII2 framework itself automatically ...