vlucas / phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
BSD 3-Clause "New" or "Revised" License
13.15k stars 626 forks source link

Variables substitution and multiple .env files #531

Closed mlocati closed 2 years ago

mlocati commented 2 years ago

Let's say we have those two .env files

# file1
HOST=one.com
URL=https://${HOST}
# file2
HOST=two.com

I load both files with

Dotenv\Dotenv::createImmutable(__DIR__', ['file1', 'file2'], false)->load();

We currently have that:

$_ENV['HOST'] === 'two.com'
$_ENV['URL'] === 'https://one.com'

Is there a way to make the variable substitution occurs after all files have been loaded? So that we have

$_ENV['HOST'] === 'two.com'
$_ENV['URL'] === 'https://two.com'
GrahamCampbell commented 2 years ago

Thanks for getting in touch. Unfortunately, no, because a variable is only "loaded" in it's final form. As to why we have to do this: your suggestion would make the loading process impossible, as users could provide reference cycles causing non-termination or intentionally use an existing value in a substitution, then change it later, like in your example.