[英]Use a function that uses a global array, within the array itself
我有以下代码:
$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
该功能工作正常。 但是我试图找到一种在数组内部正确使用它的方法。
我认为这与在调用conf函数之前未定义$ conf有关。 这些思路还可以吗?
$conf = array('site' => array(), 'url' => array());
$conf['url']['site'] = 'localhost';
$conf['url']['up'] = conf('url.site') . '/uploads';
您可以像以前一样使用所有硬定义的数组元素,但是在明确定义$ conf之后,将依赖$ conf的所有内容添加到$ conf中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.