Use the require_once question in the function to explore the elegant configuration file definition method recommendation

Source: Internet
Author: User

Background

In projects, many people prefer to use arrays in the configuration file to configure various configuration items, such as hierarchical configuration level. config. php:
Copy codeThe Code is as follows:
<? Php
$ G_levelConfig = array (
'1' => 'newbie ',
'2' => 'progress ',
);

Different modules of the project often call each other's methods and repeatedly include a file. To avoid errors, require_one is generally used, and files are often included in functions, such:
Copy codeThe Code is as follows:
Function getNameByLeval ($ level ){
$ Level = intval ($ level );
Require_once CONFIG_PATH. 'level. config. php ';
If (! Isset ($ g_levelConfig [$ level]) {
Return false;
} Else {
Return $ g_levelConfig [$ level];
}
}

Problem

So what's the problem? First look at the output of the following code. level. config. php is the configuration file mentioned above.
Copy codeThe Code is as follows:
<? Php
Function getNameByLeval ($ level ){
$ Level = intval ($ level );
Require_once 'level. config. php ';
If (! Isset ($ g_levelConfig [$ level]) {
Return false;
} Else {
Return $ g_levelConfig [$ level];
}
}
Var_dump (getNameByLeval (1 ));
Var_dump (getNameByLeval (2 ));

The output is:
Copy codeThe Code is as follows:
String (6) "Beginner"
Bool (false)

Many people find it strange that the second output is false, which is actually very simple:

Require_once only contains the file once. If the file is already included, it will not be contained again.

1. when getNameByLeval (1) is executed for the first time, the level is not included. config. php configuration file, so this time it will contain level. config. PHP file and compile. All functions contain the $ g_levelConfig variable;

2. When getNameByLeval (1) is executed for the second time, the $ g_levelConfig variable is not included because the level. config. php configuration file was previously included, and false is automatically returned;

Solution

1. Global Inclusion, which is referenced in the function
Copy codeThe Code is as follows:
<? Php
Require_once 'level. config. php'; // Add code
Function getNameByLeval ($ level ){
Global $ g_levelConfig; // Add code
$ Level = intval ($ level );
If (! Isset ($ g_levelConfig [$ level]) {
Return false;
} Else {
Return $ g_levelConfig [$ level];
}
}
Var_dump (getNameByLeval (1 ));
Var_dump (getNameByLeval (2 ));
In this case, the level. config. php configuration file must be included no matter whether the getNameByLeval function is used. It is not cost-effective.

2. Include and apply Functions
Copy codeThe Code is as follows:
<? Php
Function getNameByLeval ($ level ){
$ Level = intval ($ level );
Global $ g_levelConfig; // Add code
Require_once 'level. config. php ';
If (! Isset ($ g_levelConfig [$ level]) {
Return false;
} Else {
Return $ g_levelConfig [$ level];
}
}
Var_dump (getNameByLeval (1 ));
Var_dump (getNameByLeval (2 ));
It also seems very untidy and beautiful.

3. Use static class in the configuration file
Copy codeThe Code is as follows:
<? Php
Class levelConfig {
Public static $ level = array (
'1' => 'newbie ',
'2' => 'progress ',
);
}

When using

Copy codeThe Code is as follows:
Function getNameByLeval ($ level ){
$ Level = intval ($ level );
Require_once 'level. config. php ';
If (! Isset (levelConfig: $ level [$ level]) {
Return false;
} Else {
Return levelConfig: $ level [$ level];
}
}

I personally highly recommend this method to define configuration files. It is elegant to use and is not easy to overwrite variables.

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.