This article mainly introduces the use of require_once in functions. The elegant configuration file definition method is recommended, and a static class is used to define the configuration file to win, perfect solution to the problem of using require_once in the function. For more information, see
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:
The 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:
The 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.
The 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:
The 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
The 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
The 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
The Code is as follows:
<? Php
Class levelConfig {
Public static $ level = array (
'1' => 'newbie ',
'2' => 'progress ',
);
}
When using
The 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.