krzysztofzylka / env
ENV library
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This class is used for loading and parsing environment variables from a file.
Install
composer require krzysztofzylka/env
Load env file
$env = new \Krzysztofzylka\Env\Env(); $env->loadFromFile('/path/to/env/file');
Load multiple env file
$env = new \Krzysztofzylka\Env\Env(); $env->loadFromFile(['/path/to/env/file', '/path/to/env/file', ...]);
Load from system (getenv())
$env = new \Krzysztofzylka\Env\Env(); $env->loadFromSystem();
Example ENV file content
DB_HOST=localhost
DB_NAME=testDB
DB_USER=username
DB_PASS=password
Value conversion
Values are converted to a PHP type only when it is lossless:
| Value | Result |
|---|---|
true / false / null (any case) |
bool / null |
123, 123.456 |
int / float |
0123456, 1.50, 1.0, integers above PHP_INT_MAX |
string (converting would change the text) |
"123", '123' (quoted number) |
string |
" padded " |
string, whitespace kept |
Variables whose name ends with PASSWORD, PASSWD, PASS, SECRET, TOKEN, SECRET_KEY,
API_KEY, PRIVATE_KEY, ACCESS_KEY or AUTH_KEY (e.g. REDIS_PASSWORD, API_TOKEN) are never
converted - a password null, true or 0123456 reaches the application exactly as written.
Flags such as TOKEN_ENABLED=false are still converted to bool.
Env::isSensitiveName($name) exposes this check.
Lines may end with \n, \r\n or \r; variable names are trimmed and upper-cased.
Exceptions
File not found: If the given file path does not exist a File not found exception will be thrown.