class Dotenv

Manages .env files.

Constants

VARNAME_REGEX

STATE_VARNAME

STATE_VALUE

Methods

void
load(string $path, string ...$extraPaths)

Loads one or several .env files.

void
loadEnv(string $path, string $varName = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = array('test'))

Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.

void
overload(string $path, string ...$extraPaths)

Loads one or several .env files and enables override existing vars.

void
populate(array $values, bool $overrideExistingVars = false)

Sets values as environment variables (via putenv, $_ENV, and $_SERVER).

array
parse(string $data, string $path = '.env')

Parses the contents of an .env file.

Details

void load(string $path, string ...$extraPaths)

Loads one or several .env files.

Parameters

string $path A file to load
string ...$extraPaths A list of additional files to load

Return Value

void

Exceptions

FormatException when a file has a syntax error
PathException when a file does not exist or is not readable

void loadEnv(string $path, string $varName = 'APP_ENV', string $defaultEnv = 'dev', array $testEnvs = array('test'))

Loads a .env file and the corresponding .env.local, .env.$env and .env.$env.local files if they exist.

.env.local is always ignored in test env because tests should produce the same results for everyone. .env.dist is loaded when it exists and .env is not found.

Parameters

string $path A file to load
string $varName The name of the env vars that defines the app env
string $defaultEnv The app env to use when none is defined
array $testEnvs A list of app envs for which .env.local should be ignored

Return Value

void

Exceptions

FormatException when a file has a syntax error
PathException when a file does not exist or is not readable

void overload(string $path, string ...$extraPaths)

Loads one or several .env files and enables override existing vars.

Parameters

string $path A file to load
string ...$extraPaths A list of additional files to load

Return Value

void

Exceptions

FormatException when a file has a syntax error
PathException when a file does not exist or is not readable

void populate(array $values, bool $overrideExistingVars = false)

Sets values as environment variables (via putenv, $_ENV, and $_SERVER).

Parameters

array $values An array of env variables
bool $overrideExistingVars true when existing environment variables must be overridden

Return Value

void

array parse(string $data, string $path = '.env')

Parses the contents of an .env file.

Parameters

string $data The data to be parsed
string $path The original file name where data where stored (used for more meaningful error messages)

Return Value

array An array of env variables

Exceptions

FormatException when a file has a syntax error