Thinkphp's C Method usage example

Source: Internet
Author: User
Tags explode lowercase

  thinkphp friends know that the C () method is very common throughout the framework, the C method is very simple to implement, but the function is very powerful, the following is the C () method of understanding and using the example

1.C method   A. Load settings user's configuration, save in a C function static variable $_config   B. Read the user's configuration (read from $_congig)   2. Requirements Analysis:   1. Set variable   1. Two d array code as follows: C (' Db_password ' => ' root ', ' db_username ' => ' root '), ' DB ');   C (' DB. User_name ', ' xiaochen);       2. One-dimensional array     code is as follows: C (' user_name ', ' Xiao Chen ');   C (Array (' user_name ' => ' Chen ', ' user_height ' => ' 170 '));       2. Read variables     One dimension:   C (' user_name ');   Two-dimensional:  c (' DB. Db_password ');   3. When debugging, view all configuration information   C ();   3. Storage and why do you store it this way?   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 configuration file is only a maximum of two dimensions, the subscript lowercase of the one-dimensional array is enough   4. How to use in the actual combat?   PHP because the operation of the array is very convenient, so the configuration file is generally written in a configuration file, in the form of an array return   General format is:      code as follows: CONFIG.PHP<?  return Array (' DB ' => ' MySQL ',......);       Write variables to C &NBSP: C (include ' config.php ');     After writing, C (' DB ') can get a value of   5. The author writes   (add dynamically adds two-dimensional configuration function)   code as follows:  c (' name ' => ' mysql ', ' password ' => ') Root '), ' DB ' is executed after array (' DB ' =>array (' name ' => ' mysql ', ' password ' =>root) ')     code example:    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))? $_confi g[$name]: null;       }else{        $name = explode ('. ', $name);     &N Bsp   $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[STR ToLower ($name [0])] [$name [1]] = $value;      }       return;     &NBSP}    }elseif (Is_array ($name)) {     if (Is_null ($value))     &NBSP ; $_config = Array_merge ($_config, $name);      else{      $_config[$value] = $name;     &NBSP}      return ;    }    }else{       return empty ($_config)? null: $_config;  }}
Related Article

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.