问题的基础。我有一个配置文件位于/random_folder/ranom_f/config/_config.php
我希望能够从/random/functions/php/functions.php访问该文件
配置文件并不总是位于/random_folder/random_f/config/_config.php中,所以我必须找出一种方法来知道配置文件位于何处。
我尝试的解决方案是设置一个超全局变量(如在$_SERVER中),它给出了我正在创建的软件的"根"。
有没有人有更好的解决方案/知道我如何设置这样的超全局变量?
From Here
静态类变量可以全局引用,例如:class myGlobals {
static $myVariable;
}
function a() {
print myGlobals::$myVariable;
}
定义一个常量。根据定义,这些都是超全局变量(可用于所有作用域)
define('FOO', 'some val');
function yo() {
echo FOO;
}
下一个最好的事情是使用$GLOBALS
(不推荐,但也适用于所有作用域)
$foo = 'some val';
function yo() {
echo $GLOBALS['foo'];
}
我会使用辅助函数。首先,我认为你的配置文件路径是相对的东西,对吗?
辅助器:
function get_config_path($params){
/* code to select the path */
return 'path'; /* path is a string */
}
在你的主(index.php)文件中:
@include get_config_path($params).'/_config.php'; /* Add your standar config file name */
@include 'functions.php'
/* Use them */
这基本上就是主要思想了。