Home  >  Article  >  php教程  >  Yii framework analysis (6) - Yii alias management and object creation management

Yii framework analysis (6) - Yii alias management and object creation management

黄舟
黄舟Original
2016-12-27 11:21:211111browse

The YiiBase class provides common basic functions for the operation of the YII framework: alias management and object creation management.

When creating a php object, you need to include the definition file of this class first, and then new the object. In different environments (development environment/test environment/online environment), the configuration of apache's webroot path may be different, so the full path of the definition file of this class will be different. The Yii framework solves this problem through the alias management of YiiBase. question.

When creating an object, you need to import the definition of the corresponding class. You often need to use these five functions: include(), include_once(), require(), require_once(), set_include_path(). Yii solves this problem uniformly by using YiiBase::import(). The following figure describes the working principle of "alias management and object creation management" provided by YiiBase:

Yii framework analysis (6) - Yii alias management and object creation management

First look at alias management, which is to create a file for a folder (a folder Often corresponding to a module), create an alias. In the Yii framework, you can use this alias to replace the full path of the folder. For example: the system alias represents the path of the framework/home/work/yii/framework, so you can use system. base.CApplication represents the path to the /home/work/yii/framework/base/CApplication.php file. Of course, aliases can also be registered in the application layer code through Yii::setPathOfAlias.

Generally, we use absolute paths or relative paths for file references. Of course, both cases have disadvantages. Absolute paths: When our code is deployed to the test environment or online environment, we need to modify the paths of included files in large quantities; Relative paths: When the location of some module folders is adjusted (renamed), all relative paths All need to be modified. The way to use aliases only needs to change one thing: when registering aliases, that is, Yii::setPathOfAlias(). In this way, code changes caused by changes in folders can be concentrated in one place.

Let’s look at the import function: a. Import the definition of a class, so that objects of that class can be created; b. Add a folder to include_path, so that all files under this file can be directly included. Yii::import is equivalent to the unification of the following five functions: include(), include_once(), require(), require_once(), and set_include_path(). And generally the speed will be faster than these functions. Of course, Yii::import supports the alias function, which can solve the trouble caused by path changes.

Finally, let’s take a look at object creation. There are 2 methods to create objects in the YII framework: 1. Use the new keyword; 2. Use the Yii::createComponent method.

When using the new keyword to create an object, autoload will look for the definition of the corresponding class in three steps: a. Determine whether it is a class in the framework (all classes in the framework and the full path of this class are saved in in a member variable of YiiBase); 2. Determine whether this class has been imported using Yii::import. For non-framework classes, we need to import the definition of this class first when creating objects of this class; 3. From the include_path directory Find the php script named after this class name, so when developing, try to save the class name consistent with the file name, so that we can import the folder containing this file, so there is no need to import every file in this folder once .

When using the Yii::createComponent method to create an object, it provides more functions than the new keyword: a. Specify the location and class name of the class through the full path alias of this class (the class name must Consistent with the file name), when this class has not been imported, the definition of this class will be automatically imported based on the full path; 2. Assign values ​​to the member variables of the created object. That is, as described in the figure below, it used to take more than 3 lines of code to write, but now it can be done with one line of code (write less, do more).

Yii framework analysis (6) - Yii alias management and object creation management

The above is the Yii framework analysis (6) - the content of Yii's alias management and object creation management. 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