Is there a favored method for specifying the root folder of your website/application without having to resort to code like this:
require '../../../../config/settings.php';
I've used various methods before, and the one I'm currently considering is setting an environment variable on my host, so that I can do something like this instead:
require getenv('PROJECT_ROOT') . '/config/settings.php';
But this is really only feasible for systems with one project on them, and it has various other disadvantages, so I was wondering if there was a better method?
If you wanted to go down the environment variables path, you are able to set environment variables from your Apache vhost. Check out SetEnv and SetEnvIf in the Apache docs. e.g. To supply an environment variable to all incoming requests for a given apache vhost:
SetEnv PROJECT_ROOT='/path/to/my/project'
e.g. To change the value of an environment variable based on host name:
SetEnvIf Host www.example.com PROJECT_ROOT='/path/to/example/com'
SetEnvIf Host www.example.org PROJECT_ROOT='/path/to/example/org'
Other options include autoloading and bootstrapping. Autoloading works great for classes, but not for config files like your example. A good naming convention (such as PSR-0) will make mapping class names to file names much easier. Bootstrapping involves creating a file which you include from all of your scripts which in turn loads things like configuration settings. The bootstrap is a great place to set up autoloading. That way your bootstrap loads the configuration before the rest of the app is loaded, and you just assume it's present elsewhere in your app.