#44 - PHP Config class

Date: 2019-01-19 12:00 - PHP

Class to manage configurations for your website. It includes all PHP files from a folder to load the configurations.

<?php
class Config {
    private static $loadedFiles = false;
    private static $values = [];

    public static function set($key, $value) {
        return static::$values[$key] = $value;
    }

    public static function get($key) {
        if (!static::$loadedFiles)
            static::loadConfigs();
        return static::$values[$key] ?? null;
    }

    public static function loadConfigs() {
        if (static::$loadedFiles)
            return;
        static::$loadedFiles = true;
        $dh = opendir(__CONFIG_DIR__);
        while (($file = readdir($dh)) !== false) {
            if (substr($file, -4) == '.php' && filetype(__CONFIG_DIR__ . '/' . $file) == 'file')
                include __CONFIG_DIR__ . '/' . $file;
        }
        closedir($dh);
    }
}

Previous snippet | Next snippet