【发布时间】:2017-01-02 10:31:11
【问题描述】:
我正在使用这个 Config 类来让我更容易地读出我的偏好。
<?php
class Config {
public static function get($path = null) {
if ($path){
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach($path as $bit) {
if(isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
现在,我应该能够通过在我的脚本中使用这一行来获取配置:
echo Config::get('settings/main_color');
我的首选项位于 JSON 文件中,但存储在 $GLOBALS['config'] 中的数组如下所示:
Array (
[mysql] => Array (
[host] => localhost:3307
[username] => root
[password] => usbw
[db] => webshop )
[remember] => Array (
[cookie_name] => hash
[cookie_expiry] => 604800 )
[sessions] => Array (
[session_name] => user
[token_name] => token )
[settings] => Array (
[main_color] => #069CDE
[front_page_cat] => Best Verkocht,Populaire Producten
[title_block_first] => GRATIS verzending van €50,-
[title_block_second] => Vandaag besteld morgen in huis! )
[statics] => Array (
[header] => enabled
[title_block] => enabled
[menu] => enabled
[slideshow] => enabled
[left_box] => enabled
[email_block] => enabled
[footer] => enabled
[keurmerken] => enabled
[copyright] => enabled )
)
现在,当我尝试在我的脚本中访问一个首选项时。它说我的字符串是一个数组。所以我用 print_r 来显示数组。那么结果如下:
print_r(Config::get('settings/main_color'));
Array ( [header] => 启用 [title_block] => 启用 [menu] => 启用 [slideshow] => 启用 [left_box] => 启用 [email_block] => 启用 [footer] => 启用 [keurmerken] => 启用 [版权] => 启用)
我的脚本哪里出错了?
【问题讨论】:
-
检查你的get方法,肯定错了
标签: php arrays string oop data-conversion