When I want to use files that are "relative" to the one in which I am writing some code, I'm always using :
dirname(__FILE__)
This allows me to :
- have absolute paths that'll work everywhere, no matter to which server I deploy my application
- still use path that "look like" relative ones when I'm writting them.
For instance, to include a file that's in a "classes" sub-diretory, I would generally use :
require dirname(__FILE__) . '/classes/MyClass.php';
Note that this will work even if you're executing your script from the command-line -- while a solution based on some kind of DocumentRoot will probably not work when not using a web-server.
And, with PHP >= 5.3, you can also use the __DIR__
magic constant, which has exactly the same value as dirname(__FILE__)
, but is evaluated at compile-time, and not execution-time -- which is a bit better for performances.