今天继续阅读BaseYii.php
<?php /** * Class autoload loader. * 自动装载类加载程序。 * This method is invoked automatically when PHP sees an unknown class. * PHP用该方法自动调用一个未知类。 * 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. `yiiaseComponent`), it will attempt * to include the file associated with the corresponding path alias * (e.g. `@yii/base/Component.php`); * 如果类名称空间(如。yiiaseComponent),它将尝试包括文件与相应的路径别名(如。“@yii/base/Component.php '); * Example: When aliases `@yii` and `@yii/bootstrap` are defined, classes in the `yiiootstrap` 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. * 例子:当别名“@yii”和“@yii /引导”定义,yii 引导的名称空间中的类将加载使用“@yii /引导”别名指向的目录引导扩展 * 文件安装和所有其他类从“yii”名称空间将从yii加载框架目录。 * Also the [guide section on autoloading](guide:concept-autoloading). * * @param string $className the fully qualified class name without a leading backslash "" * @throws UnknownClassException if the class does not exist in the class file */ public static function autoload($className) { // 自动加载类 if (isset(static::$classMap[$className])) { // 如果 $classMap 中存在该类,就直接使用 $classFile = static::$classMap[$className]; // 如果第一个字符串为'@',就意味着对应的文件地址是别名,就将它转化成真实的文件地址 if ($classFile[0] === '@') { $classFile = static::getAlias($classFile); } } elseif (strpos($className, '\') !== false) { // 如果存在'\',就意味着含有 namespace,可以拼成别名,再根据别名获取真实的文件地址 $classFile = static::getAlias('@' . str_replace('\', '/', $className) . '.php', false); // 没取到真是文件地址或者获取的地址不是一个文件,就返回空 if ($classFile === false || !is_file($classFile)) { return; } } else { return; } // 引入该类的文件 include($classFile); // 如果是调试模式,而且 $className 即不是类,不是接口,也不是 trait,就抛出异常 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?"); } } /** * Creates a new object using the given configuration. * 使用给定的配置创建一个新的对象。 * You may view this method as an enhanced version of the `new` operator. * 你可能会认为这种方法是一个增强版的“新”操作符。 * The method supports creating an object based on a class name, a configuration array or * an anonymous function. * 该方法支持基于类名创建一个对象,数组或配置 * 一个匿名函数。 * * Below are some usage examples: * 以下是一些使用例子: * * ```php * // create an object using a class name * $object = Yii::createObject('yiidbConnection'); * * // create an object using a configuration array * $object = Yii::createObject([ * 'class' => 'yiidbConnection', * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo', * 'username' => 'root', * 'password' => '', * 'charset' => 'utf8', * ]); * * * @param string|array|callable $type the object type. This can be specified in one of the following forms: * @param array $params the constructor parameters * @return object the created object * @throws InvalidConfigException if the configuration is invalid. * @see yiidiContainer */ public static function createObject($type, array $params = []) { if (is_string($type)) { // 如果是一个字符串,就代表是类的名称,如:yiiwebErrorHandler return static::$container->get($type, $params); } elseif (is_array($type) && isset($type['class'])) { // 是个数组,其中的$type['class']代表类的名称 $class = $type['class']; unset($type['class']); return static::$container->get($class, $params, $type); } elseif (is_callable($type, true)) { // 是个PHP callable,那就调用它,并将其返回值作为服务或组件的实例返回 return call_user_func($type, $params); } elseif (is_array($type)) { throw new InvalidConfigException('Object configuration must be an array containing a "class" element.'); } else { throw new InvalidConfigException("Unsupported configuration type: " . gettype($type)); } } /** * Configures an object with the initial property values. * * 配置初始化一个Object,为该对象属性赋值 * * @param object $object the object to be configured * @param array $properties the property initial values given in terms of name-value pairs. * @return object the object itself */ public static function configure($object, $properties) { // 遍历配置里面的内容,一一赋值到相应的属性上 foreach ($properties as $name => $value) { $object->$name = $value; } return $object; }