env-interop / impl
Reference implementations for Env-Interop.
Requires
- php: >=8.4
- env-interop/interface: ^1.0
Requires (Dev)
- pds/composer-script-names: ^1.0
- pds/skeleton: ^1.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- pmjones/php-styler: 0.x@dev
This package is auto-updated.
Last update: 2026-07-03 16:21:39 UTC
README
Reference implementations of the Env-Interop interfaces for PHP 8.4+.
Installation
composer require env-interop/impl
Usage
Load a base environment file (INI format), with an optional local override:
use EnvInterop\Impl\EnvLoader; new EnvLoader() ->loadEnv('.env.ini') ->loadEnvIfReadable('.env.local.ini');
loadEnv() and replaceEnv() throw EnvLoaderException if a file cannot be
read; the loadEnvIfReadable() and replaceEnvIfReadable() variants suppress
that.
Read values from the environment:
use EnvInterop\Impl\Env; $env = new Env(); $pdo = new PDO( $env->getEnv('PDO_DSN'), $env->getEnv('PDO_USERNAME'), $env->getEnv('PDO_PASSWORD'), );
Add or replace environment variables:
use EnvInterop\Impl\EnvSetter; $setter = new EnvSetter(); $setter->addEnv('FEATURE_FLAG', true); // only if not already set $setter->setEnv('DEBUG', false); // always replaces; null unsets
Parse environment contents directly (INI syntax, via parse_ini_string()):
use EnvInterop\Impl\EnvParser; $parsed = new EnvParser()->parseEnv(<<<INI APP_NAME = "example" APP_DEBUG = true INI);
parseEnv() throws EnvParserException on a syntax error, or
EnvInvalidException if a parsed value is not null or scalar (such as the array
produced by key[]= syntax).
Classes
| Interface | Implementation |
|---|---|
| EnvLoaderService | EnvLoader |
| EnvParserService | EnvParser |
| EnvSetterService | EnvSetter |
| EnvGetter | Env |
| EnvLoaderThrowable | EnvLoaderException |
| EnvParserThrowable | EnvParserException |
| EnvInvalidThrowable | EnvInvalidException |
All classes are in the EnvInterop\Impl namespace.
The three exception classes extend RuntimeException and implement
EnvThrowable from the interface package, so catching that single marker
handles any environment error.
See the Env-Interop interface package for the full specification.