require_once in depth discussion on the use of require_once in functions Recommendations for elegant configuration file definition methods
- 2021-07-07 06:39:15
- OfStack
Background
Many people in projects like to use arrays in configuration files to configure individual configuration items, such as hierarchical configuration level. config. php:
<?php
$g_levelConfig = array(
'1'=>' Novice ',
'2'=>' Advanced ',
);
Because different modules of the project often call methods with each other, there will be a situation of repeatedly including one file. In order to avoid errors, everyone will use require_one, and often include files in functions, such as:
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 with this? Look at the output of the following code. level. config. php is the configuration file mentioned above
<?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:
string(6) " Novice "
bool(false)
Many people find it strange why the second output is false, which is actually very simple:
require_once contains the file only once, and if it is already included, it will not be included again.
1. When executing getNameByLeval (1) for the first time, because the level. config. php configuration file was not included before, the level. config. php file will be included and compiled this time. All functions have $g_levelConfig variables;
2. When getNameByLeval (1) is executed for the second time, because the level. config. php configuration file was included before, it is no longer included this time, so there is no $g_levelConfig variable, and false is returned naturally;
Solution
1. Globally acting on the inclusion, referencing in the function
<?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, it is not cost-effective to include the level. config. php configuration file whether the getNameByLeval function is used or not.
2. Include, apply, in a function
<?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));
This also feels very untidy and beautiful
3. Configuration file uses static class
<?php
class levelConfig{
public static $level = array(
'1'=>' Novice ',
'2'=>' Advanced ',
);
}
When in use
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 admire this way of defining configuration files, which is elegant to use and not easy to override variables.