Yii2 use spl_autoload_register to create autoload feature
yii2\Yii.php:
spl_autoload_register(['Yii', 'autoload'], true, true);
Yii::$classMap = require(__DIR__ . '/classes.php');
Yii::$container = new yii\di\Container();
classes.php file is generated, it contains a map just look like below:
return [
'yii\base\Action' => YII2_PATH . '/base/Action.php',
'yii\base\ActionEvent' => YII2_PATH . '/base/ActionEvent.php',
...
'yii\web\Application' => YII2_PATH . '/web/Application.php',
...
];
so you can using below code to create a Yii2 class.
(new yii\web\Application($config))->run(); //Yii start from here, index.php
when implement autoload function for Yii2, there also implement the alias map
to prevent hard-code absolute paths or URLs.
example:
Yii::setAlias('@foo', '/path/to/foo');
Yii::setAlias('@foo/bar', '/path2/bar');
Yii::getAlias('@foo/test/file.php'); // displays: /path/to/foo/test/file.php
Yii::getAlias('@foo/bar/file.php'); // displays: /path2/bar/file.php