In Yii2, YiiBase automatically loads classes, references file method analysis (autoload), and yii2yiibase

Source: Internet
Author: User

In Yii2, YiiBase automatically loads classes, references file method analysis (autoload), and yii2yiibase

This article analyzes how YiiBase automatically loads classes and references files in yii2. We will share this with you for your reference. The details are as follows:

In Yii2, this function is used to load classes without directly referencing the implementation of files. But this can also be used to reference files.

Public static function autoload ($ className) {/** $ classMap is a name-value array, * name is the class name, * value is the file path of this class, the path can contain the alias **. First, find whether the category name path is set in $ classMap */if (isset (static: $ classMap [$ className]). {$ classFile = static: $ classMap [$ className]; // if the path is an alias, obtain the alias path if ($ classFile [0] === '@') {$ classFile = static: getAlias ($ classFile);} // If $ className starts with "\", for example: '\ a \ B \ myclass' // the alias' @ a/B/MyClass is generated. php, and then obtain the corresponding Alias path.} Elseif (strpos ($ className ,'\\')! = False) {$ classFile = static: getAlias ('@'. str_replace ('\', '/', $ className ). '. php', false); if ($ classFile = false |! Is_file ($ classFile) {return ;}} else {return ;}// reference file include ($ classFile); // If debug occurs and the class does not exist .... // an exception occurs. // if (YII_DEBUG &&! Class_exists ($ className, false )&&! Interface_exists ($ className, false )&&! Trait_exists ($ className, false) {throw new UnknownClassException ("Unable to find '$ className' in file: $ classFile. Namespace missing? ");}}

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.