简体   繁体   中英

PHP include(): Use case for include from original script's PWD

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?

The function include() and require() handle relative path the same as any other file access function like fopen() , unlink() and mkdir() .

Path are always relative to PWD regardless of the location of the script.

Take the following example into consideration:

/home/foo/index.php does

require_once('lib/maketest.php');

/home/foo/lib/maketest.php does

mkdir('test');

This will create the directory /home/foo/test and not /home/foo/lib/test .


I can image that this has chosen because a bash script that does mkdir test will also create the directory $PWD/test regardless of where the script is located.

Having include() following the same logic as other file access functions makes sense. Consider the following code:

if (file_exists('config.php')) include('config.php');

If paths in include() would be relative to __DIR__ and paths in file_exists() to PWD the above code wouldn't work as expected.

PWD is short for Print Working Directory. Current working directory is abbreviated as CWD.
The CWD might change during the code execution and PHP doesn't necessarily have to include code from a local file path.

As you already knew, examining __FILE__ together with dirname() is the correct solution to your problem. You should also be defining a constant with the root path of your application and use that throughout the code instead of relying on that the working directory stays the same.
The CWD will not even be the path of your script starting point in all situations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM