Detailed explanation of automatic loading of Yii2 framework classes

php中世界最好的语言
Release: 2023-03-25 21:22:02
Original
1443 people have browsed it

这次给大家带来Yii2框架类自动加载使用详解,Yii2框架类自动加载的注意事项有哪些,下面就是实战案例,一起来看一下。

在yii中,程序中需要使用到的类无需事先加载其类文件,在使用的时候才自动定位类文件位置并加载之,这么高效的运行方式得益于yii的类自动加载机制。

Yii的类自动加载实际上使用的是PHP的类自动加载,所以先来看看PHP的类自动加载。在PHP中,当程序中使用的类未加载时,在报错之前会先调用魔术方法autoload(),所以我们可以重写autoload()方法,定义当一个类找不到的时候怎么去根据类名称找到对应的文件并加载它。其中autoload()方法被称为类自动加载器。当我们需要多个类自动加载器的时候,我们可以使用spl_autoload_register()方法代替autoload()来注册多个类自动加载器,这样就相当于有多个autoload()方法。spl_autoload_register()方法会把所有注册的类自动加载器存入一个队列中,你可以通过设置它的第三个参数为true来指定某个加载器放到队列的最前面以确保它最先被调用。Yii的类自动加载机制就是基于spl_autoload_register()方法的。

Yii的类自动加载机制要从它的入口文件index.php说起了,该文件源码如下:

<?php
defined(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;, true);//运行模式
defined(&#39;YII_ENV&#39;) or define(&#39;YII_ENV&#39;, &#39;dev&#39;);//运行环境
require(DIR . &#39;/../../vendor/autoload.php&#39;);//composer的类自动加载文件
require(DIR . &#39;/../../vendor/yiisoft/yii2/Yii.php&#39;);//yii的工具类文件(包含了yii类自动加载)
require(DIR . &#39;/../../common/config/bootstrap.php&#39;);//主要用于执行一些yii应用引导的代码
require(DIR . &#39;/../config/bootstrap.php&#39;);
$config = yii\helpers\ArrayHelper::merge(
  require(DIR . &#39;/../../common/config/main.php&#39;),
  require(DIR . &#39;/../../common/config/main-local.php&#39;),
  require(DIR . &#39;/../config/main.php&#39;),
  require(DIR . &#39;/../config/main-local.php&#39;)
);
(new yii\web\Application($config))->run();
Copy after login

文件中第4、5行代码分别引入了composer的类自动加载文件和yii的工具类文件Yii.php,Yii.php文件源码如下:

require(DIR . &#39;/BaseYii.php&#39;);
class Yii extends \yii\BaseYii
{
}
spl_autoload_register([&#39;Yii&#39;, &#39;autoload&#39;], true, true);//注册yii的类自动加载器
Yii::$classMap = require(DIR . &#39;/classes.php&#39;);//引入类名到类文件路径的映射
Yii::$container = new yii\di\Container();
Copy after login

这个文件定义了Yii类继承自\yii\BaseYii,代码的第6行引入了classes.php文件,该文件源码:

return [
 &#39;yii\base\Action&#39; => YII2_PATH . &#39;/base/Action.php&#39;,
 &#39;yii\base\ActionEvent&#39; => YII2_PATH . &#39;/base/ActionEvent.php&#39;,
  ....//省略n多元素
 &#39;yii\widgets\Pjax&#39; => YII2_PATH . &#39;/widgets/Pjax.php&#39;,
 &#39;yii\widgets\PjaxAsset&#39; => YII2_PATH . &#39;/widgets/PjaxAsset.php&#39;,
 &#39;yii\widgets\Spaceless&#39; => YII2_PATH . &#39;/widgets/Spaceless.php&#39;,
];
Copy after login

通过查看其源码可以看到,这个文件返回了一个从类名称到类文件路径的映射数组。这个数组被赋值给Yii::$classMap。代码的第7行调用了spl_autoload_register()方法注册了一个类自动加载器,这个类加载器为Yii::autoload(),这就是yii的类加载器了。同时这里通过把spl_autoload_register()方法第三个参数赋值为true,把yii的类加载器放在了加载器队列的最前面,所以当访问一个未加载的类的时候,yii的类自动加载器会最先被调用。

下面我们就来看看yii的类自动加载器Yii::autoload()到底做了些什么,这个方法实际上在yii\BaseYii类中,源码如下:

/**
 * 类自动加载器
 * @param type $className:要加载的类的名称
 * @return type
 * @throws UnknownClassException
 */
public static function autoload($className)
{
  if (isset(static::$classMap[$className])) {//要加载的类在 类名=>类文件路径 映射中找到
    $classFile = static::$classMap[$className];
    if ($classFile[0] === &#39;@&#39;) {//若类文件路径使用了别名,进行别名解析获得完整路径
      $classFile = static::getAlias($classFile);
    }
  } elseif (strpos($className, &#39;\\&#39;) !== false) {//类名需要包含&#39;\&#39;才符合规范
    $classFile = static::getAlias(&#39;@&#39; . str_replace(&#39;\\&#39;, &#39;/&#39;, $className) . &#39;.php&#39;, false);//进行别名解析(说明类名必须以有效的根别名打头)
    if ($classFile === false || !is_file($classFile)) {
      return;
    }
  } else {
    return;
  }
  include($classFile);//引入需要加载的类文件
  if (YII_DEBUG && !class_exists($className, false) && !interface_exists($className, false) && !trait_exists($className, false)) {
    throw new UnknownClassException("Unable to find &#39;$className&#39; in file: $classFile. Namespace missing?");
  }
}
Copy after login

这个方法首先会根据需要加载的类的名称去Yii::$classMap这个映射数组中查找,若存在则引入对应的类文件,不存在则进行别名解析得到完整文件路径,这里也说明若使用的类不在YII::$classMap中事先定义,则类名必须以有效的根别名打头,否则无法找到对应文件。

就这样,在yii中无需在程序中事先加载一大堆可能会使用到的类文件,当使用到某个类的时候,yii的类自动加载器就会自动进行加载了,高效又便捷!

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP+ajax实现获取新闻数据案例详解

php curl批处理实现可控并发异步操作案例详解

The above is the detailed content of Detailed explanation of automatic loading of Yii2 framework classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!