我有以下代码:
$conf = array(
'site' => array(
'theme' => 'master',
),
'url' => array(
'site' => 'localhost',
'up' => conf('url.site') . '/uploads',
'admin' => conf('url.site') . '/admin',
'theme' => conf('url.site') . '/theme/' . conf('site.theme'),
)
);
以及以下功能:
/**
* Return a configuration setting from the array.
* @example
* GET: conf('url.site') => $conf['url']['site'] = 'localhost'
* SET: conf('url.site', '127.0.0.1') => $conf['url']['site'] = '127.0.0.1'
*
* @param string $path the dot syntax path to resolve into the array
* @param mixed $value the value of the setting that should be set
* @return mixed the value of the setting returned
*/
function conf($path, $value = null) {
global $conf;
$config = $conf;
if($value)
$config = &$conf;
foreach (explode('.', $path) as $key) {
if($value) {
$config = &$config[$key];
continue;
}
$config = $config[$key];
}
if($value)
$config = $value;
return $config;
}
现在,我试图在函数中定义的全局数组本身中使用此函数。
如上所示,但是当我使用以下代码时:
echo conf('url.up');
它返回
/uploads
不
localhost/uploads
该功能正常。但我正试图找到一种方法在数组中正确使用它。
答案 0 :(得分:1)
我认为这与在调用conf函数之前未定义$ conf的事实有关。沿着这些方向的东西还可以吗?
$conf = array('site' => array(), 'url' => array());
$conf['url']['site'] = 'localhost';
$conf['url']['up'] = conf('url.site') . '/uploads';
您可以像以前一样使用所有硬定义的数组元素,但在明确定义$ conf后,将依赖于$ conf的内容添加到$ conf。