Class Autoloading Yii 2
Class Autoloading Yii 2

Class Autoloading

Yii relies on the class autoloading mechanism to locate and include all required class files. It provides a high-performance class autoloader that is compliant with the PSR-4 standard. The autoloader is installed when you include the Yii.php file.

Старт! Честный VDS/VPS, на котором «Лунная База» живет уже 20+ лет

Задумываетесь о том, чтобы поднять свой pet-проект, сайт клиента или настроить окружение для разработки? Уже пора подумать, где арендовать железо.

Лунная База хостится на FirstVDS более 20 лет. Мыслей о том, чтобы перебраться на другой хостинг, не было ни разу (хотя опыт работы с другими вариантами, конечно, есть).

Бывали разные ситуации, но они бывают на любом железе. Главное, что нужно понять: здесь нет маркетинговой шелухи и сказок про «мы всё сделаем за вас». Если вы берете VDS — вам дают полный root, возможность накатить любую ОС и не лезут в ваши конфиги.

Если в саппорт написать с вопросом по вашему коду и получить ответ: «Читайте документацию и разбирайтесь сами» или «Обратитесь к профильному специалисту» — это не повод обижаться. Это повод углубить свои знания в DevOps и сайтостроении! Бегите от тех, кто пообещает: «Мы всё чиним и настраиваем абсолютно бесплатно!». Никто никогда не работает «по доброте душевной» в ущерб себе, а бесплатный сыр в администрировании обычно заканчивается сломанной базой данных.

Что по факту: Гибкое масштабирование ядер и RAM, экономия по сравнению с физическими серверами и адекватная панель ispmanager, если она вам нужна.



Note: For simplicity of description, in this section we will only talk about autoloading of classes. However, keep in mind that the content we are describing here applies to autoloading of interfaces and traits as well.

Using the Yii Autoloader

To make use of the Yii class autoloader, you should follow two simple rules when creating and naming your classes:

  • Each class must be under a namespace (e.g. foo\bar\MyClass)
  • Each class must be saved in an individual file whose path is determined by the following algorithm:
// $className is a fully qualified class name without the leading backslash
$classFile = Yii::getAlias('@' . str_replace('\\', '/', $className) . '.php');

For example, if a class name and namespace is foo\bar\MyClass, the alias for the corresponding class file path would be @foo/bar/MyClass.php. In order for this alias to be resolvable into a file path, either @foo or @foo/bar must be a root alias.

When using the Basic Project Template, you may put your classes under the top-level namespace app so that they can be autoloaded by Yii without the need of defining a new alias. This is because @app is a predefined alias, and a class name like app\components\MyClass can be resolved into the class file AppBasePath/components/MyClass.php, according to the algorithm just described.

In the Advanced Project Template, each tier has its own root alias. For example, the front-end tier has a root alias @frontend, while the back-end tier root alias is @backend. As a result, you may put the front-end classes under the namespace frontend while the back-end classes are under backend. This will allow these classes to be autoloaded by the Yii autoloader.

To add a custom namespace to the autoloader you need to define an alias for the base directory of the namespace using [[Yii::setAlias()]]. For example to load classes in the foo namespace that are located in the path/to/foo directory you will call Yii::setAlias('@foo', 'path/to/foo').

Class Map

The Yii class autoloader supports the class map feature, which maps class names to the corresponding class file paths. When the autoloader is loading a class, it will first check if the class is found in the map. If so, the corresponding file path will be included directly without further checks. This makes class autoloading super fast. In fact, all core Yii classes are autoloaded this way.

You may add a class to the class map, stored in Yii::$classMap, using:

Yii::$classMap['foo\bar\MyClass'] = 'path/to/MyClass.php';

Aliases can be used to specify class file paths. You should set the class map in the bootstrapping process so that the map is ready before your classes are used.

Using Other Autoloaders

Because Yii embraces Composer as a package dependency manager, it is recommended that you also install the Composer autoloader. If you are using 3rd-party libraries that have their own autoloaders, you should also install those.

When using the Yii autoloader together with other autoloaders, you should include the Yii.php file after all other autoloaders are installed. This will make the Yii autoloader the first one responding to any class autoloading request. For example, the following code is extracted from the entry script of the Basic Project Template. The first line installs the Composer autoloader, while the second line installs the Yii autoloader:

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

You may use the Composer autoloader alone without the Yii autoloader. However, by doing so, the performance of your class autoloading may be degraded, and you must follow the rules set by Composer in order for your classes to be autoloadable.

Info: If you do not want to use the Yii autoloader, you must create your own version of the Yii.php file and include it in your entry script.

Autoloading Extension Classes

The Yii autoloader is capable of autoloading extension classes. The sole requirement is that an extension specifies the autoload section correctly in its composer.json file. Please refer to the Composer documentation for more details about specifying autoload.

In case you do not use the Yii autoloader, the Composer autoloader can still autoload extension classes for you.

Самый честный хостинг за 20+ лет существования Лунной Базы

Задумываешься о том, чтобы поднять свой сайт в Интернете? Уже пора подумать о том, какой хостинг выбрать?

Лунная База хостится 20+ лет на firstDVS. При этом, мыслей о том, чтобы перебраться на другой хостинг не было. (Но, при этом есть опыт работы с другими вариантами.)

Бывали с firstDVS разные ситуации, но, они бывают на всех хостингах. Единственное, что тут важно понять, - это то, что никто ничего тебе не будет впаривать... Но, и работать за тебя тоже никто не станет. Если в саппорт ответили: «Читайте по ссылке и разбирайтесь сами...» или вообще «Обратитесь за помощью к специалисту», - это повод углубить свои знания в области сайтостроения, а не бежать к тем, что пообещают: «Мы всё чиним и всё делаем за своих клиентов абсолютно бесплатно!» Никто никогда ничего ни за кого "по доброте душевной" не делал, не делает и не будет делать!

Старт! MoonВase — A Hot Start on the Internet
Старт! MoonВase — A Hot Start on the Internet
Старт! Menu