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

WBOY
Release: 2016-09-01 00:20:17
Original
893 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

Transfer the loading process to the first getcall for execution

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!