I think it is not good to put loadConfigFiles in the Config class, so that users will use this method, but I have to load the configuration file. How to fill up the $items inside the class?
<code><?php /** * User: 火蜥蜴制作 * Date: 2016/8/27 * Time: 8:19 * 配置类 */ namespace Core; class Config { // All of the configuration items. private static $items = []; private static $isLoaded = false; /** * 获取配置 * @param $key 如"database.dbname" * @param null $default * @return mixed */ public static function get($key, $default = null) { $params = array_filter(explode('.', $key)); $prefix = $params[0]; $key = $params[1]; if(array_key_exists($key, self::$items[$prefix])) { return self::$items[$prefix][$key]; } else { return $default; } } /** * 设置配置 * @param $key * @param $value */ public function set($key, $value) { $params = array_filter(explode('.', $key)); $prefix = $params[0]; $key = $params[1]; self::$items[$prefix][$key] = $value; } /** * 加载所有配置文件 */ public static function loadConfigFiles() { if(!self::$isLoaded) { $pattern = __DIR__ . "/../config/*.php"; $files = glob($pattern); foreach ($files as $file) { $prefix = basename($file, ".php"); self::$items[$prefix] = require($file); } self::$isLoaded = true; } } } </code>
I think it is not good to put loadConfigFiles in the Config class, so that users will use this method, but I have to load the configuration file. How to fill up the $items inside the class?
<code><?php /** * User: 火蜥蜴制作 * Date: 2016/8/27 * Time: 8:19 * 配置类 */ namespace Core; class Config { // All of the configuration items. private static $items = []; private static $isLoaded = false; /** * 获取配置 * @param $key 如"database.dbname" * @param null $default * @return mixed */ public static function get($key, $default = null) { $params = array_filter(explode('.', $key)); $prefix = $params[0]; $key = $params[1]; if(array_key_exists($key, self::$items[$prefix])) { return self::$items[$prefix][$key]; } else { return $default; } } /** * 设置配置 * @param $key * @param $value */ public function set($key, $value) { $params = array_filter(explode('.', $key)); $prefix = $params[0]; $key = $params[1]; self::$items[$prefix][$key] = $value; } /** * 加载所有配置文件 */ public static function loadConfigFiles() { if(!self::$isLoaded) { $pattern = __DIR__ . "/../config/*.php"; $files = glob($pattern); foreach ($files as $file) { $prefix = basename($file, ".php"); self::$items[$prefix] = require($file); } self::$isLoaded = true; } } } </code>
You just have to call it in the constructor
If you really don’t want users to see this method, I think you can change it to private
and then change the get
method to:
<code class="php">public static function get($key, $default = null) { $params = array_filter(explode('.', $key)); $prefix = $params[0]; $key = $params[1]; if(array_key_exists($key, self::$items[$prefix])) { return self::$items[$prefix][$key]; } else if (self::$isLoaded) { return $default; } else { self::loadConfigFiles(); } } </code>
Transfer the loading process to the first get
call for execution