Home  >  Article  >  Backend Development  >  Yii Framework Official Guide Series 12 - Basic Knowledge: Path Aliases and Namespaces

Yii Framework Official Guide Series 12 - Basic Knowledge: Path Aliases and Namespaces

黄舟
黄舟Original
2017-02-11 09:47:101476browse



# Path aliases are used extensively in Yii. A path alias is associated with the path of a directory or file. It is specified in dot syntax, similar to the widely used namespace format:

RootAlias.path.to.target

where RootAlias is an alias for an existing directory.

By using YiiBase::getPathOfAlias(), aliases can be translated to their corresponding paths . For example, system.web.CController will be translated as yii/framework/web/CController.

By calling YiiBase::setPathOfAlias(), we can define a new root path alias.

1. Root Alias

For convenience, Yii predefines the following root aliases:

  • system: Represents the Yii framework directory;

  • zii: Represents the Zii library directory;

  • application: Represents the base directory of the application;

  • webroot: Represents the directory where the entry script file is located. This alias is valid starting with version 1.0.3.

  • ext: Represents the directory containing all third-party extensions. This alias is valid starting with version 1.0.8.

In addition, if the application uses modules, (Yii) also defines a root alias for each module ID, pointing to the root directory of the corresponding module. This feature is available as of version 1.0.3.

By using YiiBase::getPathOfAlias(), aliases can be translated to their corresponding paths. For example, system.web.CController will be translated as yii/framework/web/CController.

2. Importing Classes

Using aliases can easily import class definitions. For example, if we want to include the definition of the CController class, we can call the following code


##

Yii::import('system.web.CController');

import method followed by

include is different from require and is more efficient. An imported class definition is not actually included until it is referenced for the first time. Importing the same namespace multiple times will also be much faster than include_once and require_once.

Tips: When referencing a class defined by the Yii framework, we do not need to import or include it. All core Yii classes have been imported in advance.

Using Class Map

Starting from version 1.1.5, Yii allows user-defined classes to be pre-imported by using the Class Map mechanism. This It is also the method used by Yii built-in classes. The eager import mechanism can be used anywhere in a Yii application without the need to explicitly import or include files. This feature is useful for a framework or library built on top of Yii.

To use the pre-import function, execute the following code before CWebApplication::run() is executed:


Yii::$classMap=array(
    'ClassName1' => 'path/to/ClassName1.php',
    'ClassName2' => 'path/to/ClassName2.php',
    ......
);

3. Import directory

We can also use the following syntax to import the entire directory, so that class files in this directory will be automatically included when needed.


Yii::import('system.web.*');

In addition to import, aliases point to classes in many other places. For example, a path alias can be passed to Yii::createComponent() to create an instance of the corresponding class. Even if the class file has never been included before.

4. Namespace

Don’t confuse path aliases with namespaces. A namespace refers to a logical combination of class names so that they can be distinguished from each other, even if they have the same name. The path alias is used to point to a class file or directory. Path aliases do not conflict with namespaces.

Tips: Since PHP before version 5.3.0 inherently does not support namespaces, you cannot create two instances of a class with the same name but different definitions. For this reason, all Yii framework classes are prefixed with the letter 'C' (meaning 'Class') so that they can be distinguished from user-defined classes. We recommend that the prefix 'C' be reserved only for the Yii framework, and user-defined classes be prefixed with other letters.

5. Classes using namespaces

A class using namespaces refers to a class declared in a non-global namespace. For example, the class

application\components\GoogleMap is a class under the namespace application\components. Using namespaces requires PHP 5.3.0 or above.

Starting from 1.1.5, you can use a class containing a namespace without explicitly introducing it. For example, we can create an instance of

application\components\GoogleMap without having to deal with the imported path, thus enhancing Yii's automatic import mechanism.

To automatically import classes using namespaces, the format of the namespace must be similar to the path alias. For example, the path corresponding to the class application\components\GoogleMap must be consistent with the alias application.components.GoogleMap.

The above is the Yii Framework Official Guide Series 12 - Basic Knowledge: Path Aliases and Namespaces. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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