Thinkphp C method uses the example _php instance

Source: Internet
Author: User

The role of the 1.C method

A. Load settings user's configuration, saved in a C function in the static variable $_config

B. Reading the user's configuration (read from $_congig)

2. Demand Analysis:

1. Set Variable

1. Two-D array

Copy Code code as follows:

C (Array (' Db_password ' => ' root ', ' db_username ' => ' root '), ' DB ');

C (' DB. User_name ', ' xiaochen);

2. One-dimensional arrays

Copy Code code as follows:

C (' user_name ', ' Xiao Chen ');

C (Array (' user_name ' => ' Chen ', ' user_height ' => ' 170 '));

2. Reading variables

One-dimensional: C (' user_name ');

Two-dimensional: C (' DB. Db_password ');

3. When debugging, view all the configuration information

C ();

3. How to store and why?

First we look at a problem $arr=array (' db ' => ' MySQL ', ' db ' => ' MySQL ', ' db ' => ' MySQL '); From this array we can see that DB is all pointing to MySQL, but it takes up three storage space, the development of the project is not done by one person, each person's writing habits may be different, so in order to avoid this situation, the uniform subscript to lowercase (of course, uppercase is also possible), Because the array in the config file is only a maximum of two dimensions, it's enough to lower the subscript of a one-dimensional array.

4. How to use in actual combat?

PHP is very handy for arrays, so the configuration file is typically written in a configuration file, returned as an array

The General format is:

Copy Code code as follows:

config.php<? Return Array (' DB ' => ' MySQL ',......);

Write the variable to C: C (include ' config.php '); After writing, C (' DB ') can get the value

5. The author writes (add dynamically adds two dimensional configuration function)

Copy Code code as follows:

C (Array (' name ' => ' mysql ', ' password ' => ' root '), ' DB ') executes after array (' DB ' =>array (' name ' => ' mysql ', ' password ' = >root))

code example:

Copy Code code 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;
}
}

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.