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? ");}}