The package exposes one worker class (Repository), a static facade over a
shared instance of it (DotENV), and one global helper (env()).
InitPHP\DotENV\Repository
public function create(string $path, bool $debug = true): voidReads and defines a .env or .env.php file.
$path— a path to a.env/.env.phpfile, or to a directory that contains one. When a directory is given,.envis tried first, then.env.php.$debug— whentrue(default), problems throw aDotENVException; whenfalse, problems are ignored and the method returns without defining anything.
Values already present in $_ENV or $_SERVER are not overwritten.
public function get(string $name, mixed $default = null): mixedReturns an environment value, looked up as $_ENV → $_SERVER → getenv().
Strings are coerced (value types) and ${VAR} references
are interpolated (interpolation). Returns
$default when the name is not defined anywhere.
public function env(string $name, mixed $default = null): mixedAlias of get().
public function flush(): voidRemoves every value this repository defined (from $_ENV, $_SERVER and
putenv()) and clears the read cache. Pre-existing environment variables are
left untouched.
public function drift(string|array $reference, array $options = []): DriftReportCompares the loaded environment against a $reference (a .env.example file
path, or a required-keys array) and returns an
InitPHP\DotENV\Drift\DriftReport listing missing, optional extra and
optional empty keys. Read-only — it never defines or mutates a value.
$options accepts ['extra' => bool, 'empty' => bool] (both default false).
Throws a DotENVException only when a reference file path
cannot be located or read. See environment drift for the full
treatment.
public function assertNoDrift(string|array $reference, array $options = []): voidStrict counterpart of drift(): throws a
DriftException (carrying the DriftReport) the moment any
drift is found, and returns silently when the environment is clean. For CI
gates and bootstrap guards.
InitPHP\DotENV\DotENV
A static facade that forwards create(), get(), env(), flush(),
drift() and assertNoDrift() to a single shared Repository. It adds two
lifecycle helpers:
public static function instance(): RepositoryReturns the shared repository, creating it on first use.
public static function reset(): voidFlushes the shared repository and discards it, so the next call builds a fresh one. Useful in tests and long-running workers.
use InitPHP\DotENV\DotENV;
DotENV::create(__DIR__);
DotENV::get('APP_ENV');
DotENV::flush(); // unload, keep the instance
DotENV::reset(); // unload and drop the instanceRegistered through Composer's files autoloader:
function env(string $name, mixed $default = null): mixedEquivalent to DotENV::get($name, $default) and shares the same shared
repository. It is only defined if no other env() function already exists.
$appEnv = env('APP_ENV', 'production');Also registered through Composer's files autoloader:
function env_drift(string|array $reference, array $options = []): DriftReportEquivalent to DotENV::drift($reference, $options) over the same shared
repository. Only defined if no other env_drift() function already exists. See
environment drift.
$report = env_drift(__DIR__ . '/.env.example');Before 3.0 the worker class was named InitPHP\DotENV\Lib. That name still
resolves — it is registered as an alias of Repository — but it is
deprecated. Use Repository in new code.