When using PHP's include()
and require()
functions to include a file which in turn includes another file, the PWD (relative reference) for the second file's include()
is the directory for the original script location (the first script in the stack as called by Apache), not for the current file. What is the design decision behind that, and what is the use-case?
For instance, suppose a generic database-connection class defined in ~/public_html/classes/database.php
which stores its configuration data (usernames, passwords) out of the web root in ~/config.php
. The author of the database configuration class would logically call the config file with the relative filename ../../config.php
. However, this does not work as expected because the PWD is not of the database.php
file but rather of the file which included it, which could be ~/public_html/index.php
, ~/public_html/someDir/somePage.php
or elsewhere.
I know to work around this by getting the directory of the current file with dirname(__FILE__)
. However, I cannot think of a single use case where I would want require()
or include()
to be relative to the original script location. What is the use case for it being as it is?