Thinkphp c method usage example _ php instance

Source: Internet
Author: User
Friends who have used thinkphp know that the C () method is widely used in the entire framework. The implementation of the C method is very simple, but the function is very powerful. below is the C () detailed description and Examples 1. Functions of the C method

A. Load the configuration of the Setting user and save it in the static variable $ _ config in a C function.

B. Read user configuration (read from $ _ congig)

2. Demand Analysis:

1. Set Variables

1. Two-dimensional array

The Code is as follows:


C (array ('db _ password' => 'root', 'db _ username' => 'root'), 'db ');

C ('db. USER_NAME ', 'xiaochen );

2. One-dimensional array

The Code is as follows:


C ('user _ name', 'chen ');

C (array ('user _ name' => 'chen ', 'user _ height' => '123 '));

2. Read Variables

One-dimensional: C ('user _ name ');

Two-dimensional: C ('db. DB_PASSWORD ');

3. view all configuration information during debugging

C ();

3. Storage Method and why?

First, let's look at a problem $ arr = array ('db' => 'mysql', 'db' => 'mysql', 'db' => 'mysql '); from this array, we can see that the db is directed to mysql, But it occupies three storage spaces. The development of the project is not completed by one person, and the writing habits of each person may be different, therefore, to avoid this situation, the unified subscript is converted to lowercase (of course, the upper case is also acceptable), because the configuration file in the array can only be two-dimensional at most, the lower case of the subscript of the one-dimensional array is enough.

4. How is it used in practice?

Because php is very convenient to operate arrays, the configuration file is generally written in a configuration file and returned as an array.

The general format is:

The Code is as follows:


Config. php 'Mysql ',......);

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

5. I wrote this article (the new function of dynamically adding two-dimensional configurations)

The Code is as follows:


C (array ('name' => 'mysql', 'Password' => 'root'), 'db ') array ('db' => array ('name' => 'mysql', 'Password' => root) after execution ))

Sample Code:

The Code is 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.