background
Many people in the project prefer to use arrays in configuration files to configure individual configuration items, such as hierarchical configuration level.config.php:
Copy the Code code as follows:
<?php
$g _levelconfig = Array (
' 1 ' = ' novice ',
' 2 ' = ' advanced ',
);
Because different modules of the project often call each other's methods, there is a case of repeating a file, in order to avoid errors, people generally use require_one, and often include files in the function, such as:
Copy the Code code 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 wrong with that? First look at the output of the following code, level.config.php is the configuration file mentioned above
Copy the Code code 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 the Code code as follows:
String (6) "Novice"
BOOL (FALSE)
Many people find it strange why the second output is false, which is actually very simple:
Require_once contains only one file, and if the file is already included, it will not be included again.
1. The first execution of Getnamebyleval (1) because it did not contain the level.config.php configuration file, so this time will contain level.config.php files and compiled, all functions have $g_levelconfig variables;
2. When Getnamebyleval (1) is executed for the second time, because the level.config.php configuration file was previously included, this time it is no longer included, so there is no $g_levelconfig variable, which naturally returns false;
Solutions
1. Globally acting on containment, referencing in function
Copy the Code code as follows:
<?php
Require_once ' level.config.php ';//new Code
function Getnamebyleval ($level) {
New code for global $g _levelconfig;//
$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, regardless of using the Getnamebyleval function, the level.config.php configuration file should be included in, a little less cost-effective.
2. Include, apply in functions
Copy the Code code as follows:
<?php
function Getnamebyleval ($level) {
$level = Intval ($level);
New code for global $g _levelconfig;//
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 feels so untidy and beautiful.
3. configuration file using static class
Copy the Code code as follows:
<?php
Class levelconfig{
public static $level = Array (
' 1 ' = ' novice ',
' 2 ' = ' advanced ',
);
}
When using the
Copy the Code code 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 way to define configuration files that are elegant and not easy to overwrite variables.