Is there anything wrong with having a file path as an env variable and using that as-is in a require_once?

echo DB_USER

And here is how it is set in apache:

<VirtualHost *:80>
    SetEnv CONSTANTS_FILE_PATH /path/to/file.php
    # Directory

Is there a better way of having variable const files included in the project source code?

  • \$\begingroup\$ Probably OK. But you should show us how the environment variable is set in your Apache configuration so that we can be sure. \$\endgroup\$ Commented Jul 6, 2019 at 16:29
  • \$\begingroup\$ @200_success done \$\endgroup\$
    – danielb
    Commented Jul 6, 2019 at 17:49

1 Answer 1


You are right, but there are better ways to do that. One way is using dotenv. Trending way for managing configurations.

from dotenv project:

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.

So what you are doing is right, the way you are doing may not be best.

Since you are doing require_once(), you may want to check if file exists else it would be fatal error.

The problems I see with keeping that path in Apache:

  1. You may need to modify apache config and restart / reload Apache for your new configurations
  2. Not all developers in your team may have access to Apache config. If the config is in code itself, like .env, it would be easy for developers.
  3. Debugging would be easy with .env
  4. If you plan to unit test, mocking would be easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.