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 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 indicates 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 (our) code.
In general, we use absolute or relative paths for file reference. of course, both of them 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 it to one place: when you note the 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 the definition of a class 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 alias functions, 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. Therefore, the class name should be kept 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 in the preceding example, you have to write more than three lines of code. now you can write less and do more code ).