miladm/config
Composer 安装命令:
composer require miladm/config
包简介
This package will create simple config handler for projects
README 文档
README
The Config class provides a simple way to manage configuration settings for your PHP application. You can initialize the Config object with an object, array, or file path containing your configuration data, and then retrieve individual configuration values using the get() method.
Basic usage
To get started with the Config class, create a new instance of the class by passing in your configuration data:
use miladm\Config; $config = new Config([ 'db_host' => 'localhost', 'db_port' => 3306, 'db_user' => 'myuser', 'db_pass' => 'mypass', 'db_name' => 'mydb', ]);
You can then retrieve individual configuration values using the get() method:
$host = $config->get('db_host'); $port = $config->get('db_port'); $user = $config->get('db_user'); $pass = $config->get('db_pass'); $name = $config->get('db_name');
If a configuration value is not found, the get() method will return null.
Using environment variables
You can also retrieve configuration values from environment variables. If an environment variable is defined with the same name as a configuration key, the get() method will return the value of the environment variable instead of the value from the configuration data. For example:
putenv('DB_HOST=localhost'); putenv('DB_PORT=3306'); putenv('DB_USER=myuser'); putenv('DB_PASS=mypass'); putenv('DB_NAME=mydb'); $host = $config->get('db_host'); // returns 'localhost' $port = $config->get('db_port'); // returns 3306 $user = $config->get('db_user'); // returns 'myuser' $pass = $config->get('db_pass'); // returns 'mypass' $name = $config->get('db_name'); // returns 'mydb'
more about environemt variables in different environments
Magic properties
The Config class also supports retrieving configuration values using magic properties. If a property with the same name as a configuration key is accessed, the __get() magic method will be called, which is equivalent to calling the get() method. For example:
$host = $config->db_host; $port = $config->db_port; $user = $config->db_user; $pass = $config->db_pass; $name = $config->db_name;
Caching
To improve performance, the Config class caches retrieved values in an internal array. This means that subsequent calls to get() for the same configuration key will return the cached value instead of re-fetching it from the configuration data or environment variables. If you need to clear the cache for a particular key, you can do so by setting the corresponding value in the internal cache array to null. For example:
$config->get('db_host'); // fetches from configuration data or environment variables and caches the value $config->cache['db_host'] = null; // clears the cache for the 'db_host' key $config->get('db_host'); // fetches from configuration data or
environment variables again and caches the value
Error handling
If your configuration data is not valid, the Config constructor will throw an exception. For example, if you pass in a string that is not a valid file path or JSON string, or an object or array with non-string keys, an exception will be thrown with an appropriate
miladm/config 适用场景与选型建议
miladm/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 miladm/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 miladm/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-12