ThinkPHP Function details: C method, thinkphp function details _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP Function details: C method, thinkphp function details. ThinkPHP function description: C method, thinkphp function description C method is ThinkPHP used to set, obtain, and save the configuration parameters, the usage frequency is high. To understand the C method, we need to first describe ThinkPHP functions: C method and thinkphp function

The C method is ThinkPHP's method for setting, obtaining, and saving configuration parameters, which is frequently used.
To understand the C method, you must first understand the configuration of ThinkPHP, because all operations of the C method are related to the configuration. The ThinkPHP configuration file is defined in the PHP array format.
The function overload design is used, so there are many usage instructions.
Set parameters

C('DB_NAME','thinkphp');

It indicates that the value of the DB_NAME configuration parameter is thinkphp. because the configuration parameters are case-insensitive, the following statement is the same:

C('db_name','thinkphp');

However, we recommend that you keep the configuration definition specification in uppercase.
All parameters of the project can be dynamically changed using this method before they take effect. The final value will overwrite the definitions in the previous setting or conventional configuration, you can also use the parameter configuration method to add a new configuration.
Supports second-level configuration parameter settings, for example:

C('USER.USER_ID',8);

It is not recommended that the configuration parameters exceed level 2.
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 input into an array, it indicates batch assignment. the above assignment is equivalent:

C('USER_ID',1);C('USER_TYPE',1);


GET parameters
To obtain the set parameters, you can use:

$userId = C('USER_ID');$userType = C('USER_TYPE');

If the USER_ID parameter has not been defined, NULL is returned.
You can also obtain second-level configuration parameters, for example:

$userId = C('USER.USER_ID');

If the input configuration parameter is null, all parameters are obtained:

$config = C();


Save Settings
Version 3.1 adds the function of permanently saving set parameters, which is only applicable to batch assignment. for example:

$config['user_id'] = 1;$config['user_type'] = 1;C($config,'name');

After the config parameters are set in batches, they are saved to the cache file (or other configuration cache methods) together with all the current configuration parameters ).
After saving, if you want to retrieve the saved parameters, you can use

$config = C('','name');

Here, name is the cache ID used to save the parameters. the saved parameters must be consistent before they can be retrieved correctly. The retrieved parameters are merged with the current configuration parameters.

The C method is a ThinkPHP method for setting, obtaining, and saving configuration parameters, which is frequently used. To understand the C method, you must first...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.