The Yiibase class provides a common basic function for the operation of the YII framework: Alias management and object creation management.
When you create a PHP object, you need to include the definition file for the class, and then the new object. In different environments (dev/test/online), Apache's Webroot path may be configured differently, so the full path to the definition file for this class will be different, and the YII framework solves this problem by Yiibase alias management.
When you create an object, you need to import the definition of the corresponding class, and you often need to use these 5 functions: include (), include_once (), require (), require_once (), Set_include_path (). Yii solves this problem uniformly by using yiibase::import (). Describes how Yiibase provides "Alias management and object creation management":
First look at alias management, it is by a folder (a folder often corresponds to a module) an alias, in the YII framework can use this alias to replace the full path of this folder, such as: The system alias represents the framework/home/work/yii/ The path to the framework, so you can use System.base.CApplication to represent the path to the/home/work/yii/framework/base/capplication.php file. Of course, you can also register aliases with Yii::setpathofalias in the application layer's code.
In general, we use absolute paths or relative paths for file references, although both cases have drawbacks. Absolute path: When our code is deployed to the test environment or the online environment, we need to modify the path of the include file extensively, relative path: When the folder location of some modules changes (renaming), all relative paths need to be modified. The way to use aliases is to change only one place: when registering aliases, that is, Yii::setpathofalias (). The code churn resulting from the changes in the folder is then concentrated in one place.
Then look at the import function: A, import the definition of a class, so that the object of the class can be created, B, add a folder to Include_path, so that you can directly include all the files under this file. Yii::import corresponds to the unification of the following 5 functions: include (), include_once (), require (), require_once (), Set_include_path (). and generally faster than these functions. Of course, Yii::import supports the function of aliases, which can solve the problem caused by the path change.
Finally look at the creation of the object, in the YII framework there are 2 methods to create objects: 1, using the new keyword, 2, using the Yii::createcomponent method.
When creating an object using the New keyword, autoload will look for the definition of the corresponding class in 3 steps: A, determine whether the class is in the framework (all classes of the framework and the full path of the class are stored in a member variable of yiibase); 2. Determine if Yii is used : Import imports This class, for non-framework classes, we need to import the class's definition of the class, 3, from the Include_path directory to find a PHP script named after this class name, so in the development of the class name as far as possible to save the file name consistent, This allows us to import the folder containing this file, so we don't have to import every file in this folder once.
When you use the Yii::createcomponent method to create an object, it provides more functionality than the New keyword: A, specifying the class's location and class name through the class's full path alias (the class name must match the file name), and when the class has not been imported, The definition of the class is automatically imported according to the full path, and 2 is assigned to the member variables of the created object. As described, the original is to write more than 3 lines of code, now a line of code can be done (write less, does more).
Yii Framework Analysis (VI) Alias management and object creation management of--yii