函数中使用require_once问题深入探讨 优雅的配置文件定义方法推荐
背景
在项目中很多人喜欢在配置文件使用数组来配置各个配置项,如等级的配置level.config.php:
复制代码 代码如下:
$g_levelConfig = array(
'1'=>'新手',
'2'=>'进阶',
);
因为项目不同模块经常会相互调用方法,会出现重复包含一个文件的情况,为了避免错误,大家一般都会用require_one,而且经常会在函数里包含文件,如:
复制代码 代码如下:
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];
}
}
问题
那么这样会有什么问题呢?先看如下代码的输出,level.config.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));
输出是:
复制代码 代码如下:
string(6) "新手"
bool(false)
很多人觉得很奇怪,为什么第二次输出的是false,其实很简单:
require_once只包含一次文件,如果该文件已经被包含了,则不会再次包含。
1.第一次执行getNameByLeval(1)时因为之前没包含level.config.php配置文件,所以本次会包含level.config.php文件并编译,所有函数里有$g_levelConfig变量;
2.当第二次执行getNameByLeval(1)时,因为之前包含过level.config.php配置文件,本次不再包含,所以就没有$g_levelConfig变量,自然返回false;
解决办法
1.在全局作用于包含,在函数中引用
复制代码 代码如下:
require_once 'level.config.php';//新增代码
function getNameByLeval($level){
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));
这样的话,无论用不用getNameByLeval函数,都要把level.config.php配置文件包含进来,有点不划算。
2.在函数中包含、应用
复制代码 代码如下:
function getNameByLeval($level){
$level = intval($level);
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));
这样也感觉很不整洁美观
3.配置文件使用静态class
复制代码 代码如下:
class levelConfig{
public static $level = array(
'1'=>'新手',
'2'=>'进阶',
);
}
使用的时候
复制代码 代码如下:
function getNameByLeval($level){
$level = intval($level);
require_once 'level.config.php';
if(!isset(levelConfig::$level[$level])){
return false;
}else{
return levelConfig::$level[$level];
}
}
我个人非常推崇这个方式来定义配置文件,用起来优雅而且不容易覆盖变量。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
