# sg-config **Repository Path**: iceson5/php-config ## Basic Information - **Project Name**: sg-config - **Description**: php动态加载config配置文件,静态存储,动态获取或设置 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-03-02 - **Last Updated**: 2023-03-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # php config #### 项目介绍 - php动态加载config配置文件,静态存储,动态获取或设置 - 支持无限级数组设置 - 支持无限级节点获取 - 59行代码,一个函数搞定,实现配置无限级动态加载 #### 目录说明 - functions.php 函数文件 - config/*.php 实例文件 - index.php 测试文件 #### 安装教程 1. 引用functions.php 2. 运行index.php #### 使用说明 1. 文件加载方式 config(include('file.php')); 2. 设置临时配置 config('php.name', 'php5.6'); 3. 动态获取 config('php.name'); 4. 区分大小写,使用config() 推荐使用 5. 不区分大小写,使用configCase() #### 核心函数 ```php /** * 存储健区分大小写 * 获取和设置配置参数 支持批量定义,支持无限层 * * 加载文件:config(include($file));支持多次加载 * 加载文件:config(include($file), '键(文件名)');设置以文件名为键 * 设置临时参数:config('a.b.c', 'abc'); * * 获取所有的参数:config(); * 获取单个值:config('a.b.c'); * * @param string|array $name 配置变量 * @param mixed $value 配置值 * @param mixed $default 默认值 * @see https://gitee.com/sgfoot/php-config * @author freelife2020@163.com * @return mixed */ function config($name = null, $value = null, $default = null) { static $_config = array(); if (!function_exists('array_get')) { /** * 获取key值,递归 * @param $str string 以.隔开 * @param $array array 多维数组 * @return mixed|null */ function array_get($str, $array) { $list = explode('.', $str); $first = array_shift($list); $result = isset($array[$first]) ? $array[$first] : null; if ($result && is_array($result) && count($list) > 0) { return array_get(join('.', $list), $result); } return $result; } } if (!function_exists('array_set')) { /** * 数组设置动态值 * @param $str string 以.隔开的字符串 * @param $val mixed 值 * @param $array array 引用,目标数据 * @return array */ function array_set($str, $val, &$array) { $list = explode('.', $str); $first = array_shift($list); if (count($list) > 0) { return array_set(join('.', $list), $val, $array[$first]); } $array[$first] = $val; } } // 无参数时获取所有 if (is_null($name)) return $_config; // 优先执行设置获取或赋值 if (is_string($name)) { $name = explode('.', $name); //获取值 if (is_null($value)) { $rs = array_get(join('.', $name), $_config); return is_null($rs) ? $default : $rs; } //设置值 array_set(join('.', $name), $value, $_config); return null; } //批量设置 if (is_array($name)) { if (is_null($value)) { $_config = array_merge($_config, $name); } else { $_config = array_merge($_config, array($value => $name)); } return null; } return null; // 避免非法参数 } ```