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?

&lt;?
$CONSTANTS_FILE = getenv('CONSTANTS_FILE_PATH');
require_once($CONSTANTS_FILE);
echo DB_USER
?&gt;


And here is how it is set in apache:

&lt;VirtualHost *:80&gt;
    #ServerName 
    #DocumentRoot
    SetEnv CONSTANTS_FILE_PATH /path/to/file.php
    # Directory
&lt;/VirtualHost&gt;


Is there a better way of having variable const files included in the project source code?
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:


You may need to modify apache config and restart / reload Apache for your new configurations
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.
Debugging would be easy with .env
If you plan to unit test, mocking would be easy.