In-depth discussion on the use of require_once in functions. Recommended elegant configuration file definition method_PHP tutorial

WBOY
Release: 2016-07-13 10:26:26
Original
778 people have browsed it

Background

In projects, many people like to use arrays in configuration files to configure various configuration items, such as level configuration level.config.php:

Copy code The code is as follows:

$g_levelConfig = array(
'1'=>'Newbie',
'2'=>'Advanced',
);

Because different modules of the project often call each other's methods, a file may be included repeatedly. In order to avoid errors, everyone generally uses require_one, and often includes files in functions, such as:
Copy code 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];
}
}

Question

So what’s the problem with this? First look at the output of the following code. level.config.php is the configuration file mentioned above

Copy code The code is as follows:

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 code The code is as follows:

string(6) "Newbie"
bool(false)

Many people find it strange why the second output is false. It is actually very simple:

require_once only includes the file once. If the file 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. There is a $g_levelConfig variable in all functions;

2. When getNameByLeval(1) is executed for the second time, because the level.config.php configuration file was previously included and is no longer included this time, there is no $g_levelConfig variable and naturally returns false;

Solution

1. Acts on inclusion globally and references
in functions

Copy code The code is as follows:

require_once 'level.config.php';//New code
function getNameByLeval($level){
Global $g_levelConfig;//New 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, regardless of whether the getNameByLeval function is used or not, the level.config.php configuration file must be included, which is a bit uneconomical.

2. Include and apply
in functions

Copy code The code is as follows:

function getNameByLeval($level){
$level = intval($level);
Global $g_levelConfig;//New 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. The configuration file uses static class

Copy code The code is as follows:

class levelConfig{
Public static $level = array(
'1'=>'Newbie',
'2'=>'Advanced',
);
}

When using it

Copy code 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 way to define configuration files, it is elegant to use and not easy to overwrite variables.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824669.htmlTechArticleBackground In projects, many people like to use arrays in configuration files to configure various configuration items, such as level configuration levels. config.php: Copy the code as follows: php $g_levelConfig = array(...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!