Home >Backend Development >PHP Tutorial >Detailed explanation of ThinkPHP functions: C method
Method C is the method used by ThinkPHP to set, obtain, and save configuration parameters, and is used more frequently.
To understand the C method, you need to first understand the configuration of ThinkPHP, because all operations of the C method are related to the configuration. ThinkPHP's configuration file is defined in PHP array format.
Due to the function overloading design, there are many uses. Let’s explain them one by one.
Set parameters
C('DB_NAME','thinkphp');
means setting the value of the DB_NAME configuration parameter to thinkphp. Since the configuration parameters are not case-sensitive, the following writing is the same:
C('db_name','thinkphp');
However, it is recommended to maintain the configuration definition specifications in uniform capital letters.
All parameters of the project can be dynamically changed through this method before taking effect. The last set value will overwrite the definition in the previous settings or conventional configuration. You can also use the parameter configuration method to add new configurations.
Supports the setting of secondary configuration parameters, for example:
C('USER.USER_ID',8);
It is not recommended that configuration parameters exceed the second level.
If you want to set multiple parameters, you can use batch settings, for example:
$config['user_id'] = 1; $config['user_type'] = 1; C($config);
If the first parameter of the C method is passed into the array, it means batch assignment. The above assignment is equivalent to:
C('USER_ID',1); C('USER_TYPE',1);
Get parameters
To get The parameters to be set can be used:
$userId = C('USER_ID'); $userType = C('USER_TYPE');
If the USER_ID parameter has not been defined, NULL will be returned.
It can also support obtaining secondary configuration parameters, for example:
$userId = C('USER.USER_ID');
If the incoming configuration parameters are empty, it means getting all parameters:
$config = C();
Save settings
Version 3.1 adds a function to permanently save setting parameters, only In the case of batch assignment, for example:
$config['user_id'] = 1; $config['user_type'] = 1; C($config,'name');
After setting the config parameters in batches, all current configuration parameters will be saved to the cache file (or other configured caching methods).
After saving, if you want to retrieve the saved parameters, you can use
$config = C('','name');
where name is the cache identifier used when saving the parameters earlier. It must be consistent to correctly retrieve the saved parameters. The retrieved parameters will be merged with the current configuration parameters, without manual merging.
The above introduces the detailed explanation of ThinkPHP functions: C method, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.