chillerlan/php-dotenv

简单的 .env 加载器 - PHP 8.1+

3.0.1 2024-03-07 20:36 UTC

This package is auto-updated.

Last update: 2024-09-07 21:38:26 UTC


README

.env 文件加载内容到环境。PHP 8.1+

PHP Version Support version license Coverage Packagist downloads CI

文档

安装

需要 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