Yii2 deep learning-automatic loading mechanism

Source: Internet
Author: User
: This article mainly introduces Yii2's deep learning-automatic loading mechanism. For more information about PHP tutorials, see. The automatic loading of Yii2 is divided into two parts: one is the automatic loading mechanism of Composer, and the other is the automatic loading mechanism of Yii2 framework.

Automatic Composer loading

Composer generatesvendor/autoload.phpFile. You can simply introduce this file and you will get an automatic loading support.

In the previous article, we can see the following content in the portal file:

// Introduce the autoload. php file in the vendor to automatically load the class based on the composer mechanism.Require(_ DIR _. '/../vendor/autoload. php ');

This series is mainly about Yii2, so the automatic Composer loading mechanism is not described here.

Available materials:

  1. Automatic Composer loading
  2. Automatic Composer loading-reference
  3. Composer official website

Automatic loading mechanism of Yii2 framework

The automatic loading of the Yii2 framework is implemented through the spl_autoload_register method.

In the previous article, we can see the following content in the portal file:

// Introduce the Yii Framework file Yii. phpRequire(_ DIR _. '/../vendor/yiisoft/yii2/Yii. php ');

What is in Yii. php? How does one implement automatic loading?

The Yii. php content is as follows:

 Require(_ DIR __. '/BaseYii. php ');/*** Yii is a helper class serving common framework functionalities. ** It extends from [[\ yii \ BaseYii] which provides the actual implementation. * By writing your own Yii class, you can customize some functionalities of [[\ yii \ BaseYii]. ** @ author Qiang Xue
 
  
* @ Since 2.0 */class Yii extends \ yii \ BaseYii {}/ *** spl_autoload_register-register the given function as the _ autoload implementation ** bool spl_autoload_register ([callable $ autoload_function [, bool $ throw = true [, bool $ prepend = false]) ** register the function to the SPL _ autoload function
  Queue. If
  QueueIf the functions in are not activated, activate them. * If the _ autoload () function has been implemented in your program, it must be explicitly registered to _ autoload ()
  Queue. * Because the spl_autoload_register () function replaces the _ autoload () function in Zend Engine with spl_autoload () or spl_autoload_call (). * If multiple autoload functions are required, spl_autoload_register () meets these requirements. * It actually creates the autoload function
  QueueIn the defined sequence. * In contrast, _ autoload () can be defined only once. ** Autoload_function * indicates the automatic loading function to be registered. If no parameter is provided, the default implementation function spl_autoload () of autoload is automatically registered (). ** Throw * this parameter sets whether spl_autoload_register () throws an exception when autoload_function cannot be successfully registered. ** Prepend * if it is true, spl_autoload_register () will add the function
  QueueFirst, not
  QueueTail. ** Yii registers the Yii autoload function for automatic loading. In fact, */spl_autoload_register (['yii', 'autoload'], true, true) in \ yii \ BaseYii ); // define the class name of the Yii core class and the MapYii: $ classMap = of the file address
  Require(_ DIR _. '/classes. php'); // create the Yii dependency injection container Yii: $ Container = new yii \ di \ container ();
 

The main content is to introduce the BaseYii. php file, declare the Yii class, inherit the BaseYii, and then register the autoload method of Yii (actually BaseYii) to implement automatic loading. Then, a Map corresponding to the Yii core class name and file address is introduced and stored in Yii ::$ classMap. Finally, an instance of yii \ di \ Container is created and stored in Yii ::$ container.

We can see that the key code for automatic loading is:

spl_autoload_register(['Yii', 'autoload'], true, true);

Let's take a look at the implementation of the autoload method in BaseYii. its content is as follows:

/*** Class autoload loader. * This method is invoked automatically when PHP sees an unknown class. * The method will 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 will attempt * to include the file associated with the corresponding path alias * (e.g. '@ yii/base/Component. php '); ** This autoloader allows loading classes that follow the [PSR-4 standard] ( http://www.php-fig.org/psr/psr-4/ ) * And have its top-level namespace or sub-namespaces defined as path aliases. ** Example: When aliases '@ yii' and' @ yii/bootstrap 'are defined, classes in the 'yii \ bootstrap 'namespace * will be loaded using the '@ yii/bootstrap' alias which points to the directory where bootstrap extension * files are installed and all classes from other' yii 'namespaces will be loaded from the yii framework directory. ** Also the [guide section on autoloading] (guide: concept-autoloading ). ** @ param string $ className the fully qualified class name without a leading backslash "\" * @ throws UnknownClassExceptionIf the class does not exist in the class file */publicstaticfunction autoload ($ className) {// automatically load the class if (isset (static: $ classMap [$ className]) {// if $ classMap contains this class, use $ classFile = static: $ classMap [$ className] directly; // if the first string is '@', this means that the corresponding file address is an alias and is converted into a real file address. if ($ classFile [0] ===' @ ') {$ classFile = static :: getAlias ($ classFile) ;}} elseif (strpos ($ className ,'\\')! = False) {// If '\' exists, it means that it contains namespace. you can create an alias and obtain the actual file address $ classFile = static :: getAlias ('@'. str_replace ('\', '/', $ className ). '. php ', false); // if the actual file address is not obtained or the obtained address is not a file, an empty if ($ classFile = false |! Is_file ($ classFile) {return ;}} else {return ;}// introduce the file include ($ classFile) for this class; // if the debugging mode is used, in addition, $ className is neither a class nor an interface nor a trait. if (YII_DEBUG &&! Class_exists ($ className, false )&&! Interface_exists ($ className, false )&&! Trait_exists ($ className, false) {thrownew UnknownClassException("Unable to find '$ className' in file: $ classFile. Namespace missing? ");}}

You may not know the getAlias method. this method is to convert the alias in Yii2 to the actual file address. the specific content of this method will be explained in detail later.

Here are some examples to help you understand.

If the Yii ::$ classMap value is as follows:

Yii::$classMap = [    'app/test/Test' => '/var/www/basic/webtest/Test.php'];

When you use the 'app/test/test' class, '/var/www/basic/webtest/Test will be automatically introduced. php' file, the project content is certainly not like this, this is just a simple example for everyone to understand.

In the above example, if you use the 'yii \ base \ component' class, it will be converted into '@ yii/base/Component. php 'Alias, and then get its file address based on the alias, introduce it.

The above is the basic content of Yii2's automatic loading mechanism ~~

If you are interested in Yii2 source code, you can pay attention to the project yii2-2.0.3-annotated. now you have added a lot of comments about Yii2 source code, and will continue to add ~

If you are interested, you can also participate in the submission of comments from the Yii2 source code.

The above describes Yii2's deep learning-automatic loading mechanism, including The require, queue, and Exception content. I hope to help anyone who is interested in PHP tutorials.

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.