chillerlan / php-dotenv
简单的 .env 加载器 - PHP 8.1+
3.0.1
2024-03-07 20:36 UTC
Requires
- php: ^8.1
Requires (Dev)
- phan/phan: ^5.4
- phpunit/phpunit: ^10.5
README
从 .env
文件加载内容到环境。PHP 8.1+
文档
安装
需要 composer
composer.json (注意:用 版本约束 替换 dev-main
,例如 ^3.0
)
{ "require": { "php": "^8.1", "chillerlan/php-dotenv": "dev-main" } }
通过终端安装: composer require chillerlan/php-dotenv
利润!
用法
# example .env
FOO=bar
BAR=foo
WHAT=${BAR}-${FOO}
$env = new DotEnv(__DIR__.'/../config', '.env'); $env->load(['foo']); // foo is required // get a variable $foo = $_ENV['FOO']; // -> bar $foo = $env->get('FOO'); // -> bar $foo = $env->FOO; // -> bar // dynamically set a variable $env->set('foo', 'whatever'); $env->FOO = 'whatever'; $foo = $env->get('FOO'); // -> whatever // ... // variable substitution $foo = $env->get('WHAT'); // -> foo-bar
// avoid the global environment $env = (new DotEnv(__DIR__.'/../config', '.env', false))->load(); $foo = $_ENV['FOO']; // -> undefined $foo = $env->get('FOO'); // -> bar $foo = $env->FOO; // -> bar