# PHP/Apache - Production/staging consts file path

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

<?
$CONSTANTS_FILE = getenv('CONSTANTS_FILE_PATH'); require_once($CONSTANTS_FILE);
echo DB_USER
?>


And here is how it is set in apache:

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


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

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

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.