Friends who have used thinkphp know that the C () method is widely used in the entire framework. the implementation of the C method is very simple, but the function is very powerful. below is the C () detailed description and example of ThinkPHP
1. functions of the C method
A. load the configuration of the setting user and save it in the static variable $ _ config in a C function.
B. read User configuration (read from $ _ congig)
2. demand analysis:
1. set variables
1. two-dimensional array
The code is as follows:
C (array ('Db _ password' => 'root', 'DB _ username' => 'root'), 'DB ');
C ('Db. USER_NAME ', 'xiaochen );
2. one-dimensional array
The code is as follows:
C ('User _ name', 'Chen ');
C (array ('User _ name' => 'Chen ', 'User _ height' => '123 '));
2. read variables
One-dimensional: C ('User _ name ');
Two-dimensional: C ('Db. DB_PASSWORD ');
3. View all configuration information during debugging
C ();
3. storage method and why?
First, let's look at a problem $ arr = array ('DB' => 'mysql', 'DB' => 'mysql', 'DB' => 'mysql '); from this array, we can see that the db is directed to mysql, but it occupies three storage spaces. The development of the project is not completed by one person, and the writing habits of each person may be different, therefore, to avoid this situation, the unified subscript is converted to lowercase (of course, the upper case is also acceptable), because the configuration file in the array can only be two-dimensional at most, the lower case of the subscript of the one-dimensional array is enough.
4. how is it used in practice?
Because php is very convenient to operate arrays, the configuration file is generally written in a configuration file and returned as an array.
The general format is:
The code is as follows:
Config. php 'Mysql ',......);
Write the variable to C: C (include 'config. php'); after writing, C ('DB') can get the value.
5. I wrote this article (the new function of dynamically adding two-dimensional configurations)
The code is as follows:
C (array ('name' => 'mysql', 'password' => 'root'), 'DB ') array ('DB' => array ('name' => 'mysql', 'password' => root) after execution ))
Sample code:
The code is as follows:
Function C ($ name = null, $ value = null ){
Static $ _ config = array ();
If (! Is_null ($ name )){
If (is_string ($ name )){
If (is_null ($ value )){
If (! Strpos ($ name ,'.')){
$ Name = strtolower ($ name );
Return isset ($ _ config [$ name])? $ _ Config [$ name]: null;
} Else {
$ Name = explode ('.', $ name );
$ Name [0] = strtolower ($ name [0]);
Return isset ($ _ config [$ name [0] [$ name [1])? $ _ Config [$ name [0] [$ name [1]: null;
}
} Else {
If (! Strpos ($ name ,'.')){
$ _ Config [strtolower ($ name)] = $ value;
} Else {
$ Name = explode ('.', $ name );
$ _ Config [strtolower ($ name [0])] [$ name [1] = $ value;
}
Return;
}
} Elseif (is_array ($ name )){
If (is_null ($ value ))
$ _ Config = array_merge ($ _ config, $ name );
Else {
$ _ Config [$ value] = $ name;
}
Return;
}
} Else {
Return empty ($ _ config )? Null: $ _ config;
}
}