Home >Backend Development >PHP Tutorial >PHP design problem, I wrote a Config class to read the configuration file

PHP design problem, I wrote a Config class to read the configuration file

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-09-01 00:20:17957browse

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>

Reply content:

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 getcall for execution

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php bubble sort methodNext article:php bubble sort method