Yii Framework Analysis (6) -- alias management and object creation management of Yii
The YiiBase class provides common basic functions for running the YII Framework: Alias management and object creation management.
When creating a php object, you must first include the definition file of this class, and then new this object. In different environments (development environment, test environment, and online environment), apache's webroot path configuration may be different, so the full path of the definition file of this class will be different, yii Framework solves this problem through the alias management of YiiBase.
When creating an object, you need to import the definition of the corresponding class. these five functions are often used: include (), include_once (), require (), require_once (), and set_include_path (). Yii solves this problem by using YiiBase: import. Describes how YiiBase provides "alias management and object creation management:
First, let's look at alias management. it creates an alias for a folder (a folder usually corresponds to a module). in the Yii Framework, this alias can be used to replace the full path of this folder, for example, the system alias represents the path of the framework/home/work/yii/framework, so you can use system. base. CApplication indicates/home/work/yii/framework/base/CApplication. php file path. Of course, you can also use Yii: setPathOfAlias to register aliases in the application layer code.
In general, we use absolute or relative paths for file reference. of course, these two situations have drawbacks. Absolute path: when our code is deployed to a test or online environment, the path of the include file needs to be greatly modified; relative path: When the folder location of some modules is adjusted (renamed) all relative paths must be modified. To use an alias, you only need to change one place: when registering an alias, that is, Yii: setPathOfAlias (). In this way, code changes caused by folder changes are centralized in one place.
Let's look at the import function again: a. import a class definition to create the class object. B. add a folder to include_path to include all the files in this file. Yii: import is equivalent to the following five functions: include (), include_once (), require (), require_once (), set_include_path (). In general, the speed is 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. in the YII Framework, there are two methods to create objects: 1. use the new keyword; 2. use the Yii: createComponent method.
When the new keyword is used to create an object, autoload searches for the definition of the corresponding class in three steps: a. determine whether it is a class in the framework (all classes of the framework and the full path of this class are saved in a Member variable of YiiBase); 2. determine whether Yii is used :: import imports this class. for non-framework classes, we need to first import the definition of this class when creating the class object; 3. search for the php script named after this class in the include_path directory, so the class name should be consistent with the file name during development, in this way, you can import the folder containing this file, so that you do not need to import every file in this folder once.
When you use the Yii: createComponent method to create an object, it provides more functions than the new keyword: a. Use the full path alias of the class to specify the class location and class name (the class name must be consistent with the file name). when the class has not been imported, the class definition is automatically imported according to the full path. 2. assign values to the member variables of the created object. As described above, three or more lines of code have to be written, and now one line of code can be done (write less, do more ).