The C method is thinkphp used to set up, get, and save the configuration parameters of the method, the use of high frequency.
Understanding the C method requires first understanding of the next thinkphp configuration, since all operations of the C method are related to configuration. The thinkphp configuration file is defined using the PHP array format.
Because of the use of function overload design, so the use of more, we come to one by one notes.
Setting parameters
C (' db_name ', ' thinkphp ');
Indicates that the value of setting the Db_name configuration parameter is thinkphp, because the configuration parameters are not case-sensitive, so the following writing is the same:
C (' db_name ', ' thinkphp ');
However, it is recommended that you maintain a uniform capitalization configuration definition specification.
All parameters of a project can be dynamically changed by this method before it takes effect, and the last set value overrides the definition in the previous or custom configuration, or you can add a new configuration using the parameter configuration method.
Supports the settings for level two configuration parameters, such as:
C (' USER. USER_ID ', 8);
Configuration parameters do not recommend more than two levels.
If you want to set multiple parameters, you can use the batch setting, for example:
$config [' user_id '] = 1;
$config [' user_type '] = 1;
C ($config);
If the first parameter of the C method passes in an array, it represents a bulk assignment, which is equivalent to the following assignment:
C (' user_id ', 1);
C (' User_type ', 1);
Get parameters
To get the parameters of the setting, you can use:
$userId = C (' user_id ');
$userType = C (' User_type ');
Returns null if the USER_ID parameter has not been defined.
You can also support obtaining level two configuration parameters, such as:
$userId = C (' USER. User_ID ');
If the incoming configuration parameter is empty, the parameters are obtained:
Save Settings
The 3.1 version adds a feature that permanently saves settings parameters, only for bulk assignments, such as:
$config [' user_id '] = 1;
$config [' user_type '] = 1;
C ($config, ' name ');
After the config parameter is set in bulk, it is saved to the cached file (or other configured caching mode) along with all current configuration parameters.
After saving, if you want to retrieve the saved parameters, you can use the
$config = C (', ' name ');
Where name is the identity of the cache used to save the parameter earlier, it must be consistent to retrieve the saved parameters correctly. The retrieved parameters are merged with the current configuration parameters and do not need to be manually merged.
The code is as follows:
Class Testaction extends action{
/**
* $config [' user_id '] = 1;
$config [' user_type '] = 1;
C ($config);
If the first parameter of the C method passes in the array, it represents a batch assignment, which is equivalent to:
C (' user_id ', 1);
C (' User_type ', 1);
Get parameters
to get settings for the parameter, you can use:
$userId = C (' user_id ');
$userType = C (' User_type ');
* * Public
Function Index () {
C (' user_id ', 102); Assign value to
the parameter C (' User_type ', 107);//Assign a value to the parameter
dump (C (' user_id ')). ' <br/> ';//102=> gets the set parameter
dump (C (' User_type ')). ' <br/> ';//107=> Get Set parameters
$this->display ();
}