30

For example, I always see autoloaders called like this:

require_once __DIR__ . '/../vendor/autoload.php';

What is the difference between that and the more concise

require_once '../vendor/autoload.php';

?

4 Answers 4

39

PHP scripts run relative to the current path (result of getcwd()), not to the path of their own file. Using __DIR__ forces the include to happen relative to their own path.

To demonstrate, create the following files (and directories):

- file1.php
- dir/
   - file2.php
   - file3.php

If file2.php includes file3.php like this:

include `file3.php`.

It will work fine if you call file2.php directly. However, if file1.php includes file2.php, the current directory (getcwd()), will be wrong for file2.php, so file3.php cannot be included.

11

The current accepted answer does not fully explain the cause of using __DIR__ and in my opinion the answer is wrong. I am gonna explain why do we really need this. Suppose we have a file structure like this

- index.php
- file3.php -(content: hello fake world)
- dir/
   - file2.php
   - file3.php - (content: hello world)

If we include file3.php from file2.php and run file2.php directly, we will see the output hello world. Now when we include file2.php in index.php, when the code will start executing and it will see file2.php again including file3.php using include 'file3.php', at first the execution will look for file3.php in the current execution directory (which is the same directory where index.php is present)..Since file3.php is present in that directory, it will include that file3.php instead of dir/file3.php and we will see the output hello fake world instead of hello world.

If file3.php would not exist in the same directory, it would then include the correct dir/file3.php file which makes the accepted answer not valid because it states file3.php cannot be included which is not true. It is included.

However, here comes the necessity of using __DIR__. If we would use include __DIR__ . '/file3.php' in file2.php, then it would include the correct file even though another file3.php is present in the parent directory.

0

For include its possible to set some folders where PHP search automatically. When you include a file with a relative path you search in all of that folders. Its better to define the real path to prevent some errors in loading wrong files.

https://secure.php.net/manual/en/function.set-include-path.php

Then you can be sure that you load the correct file.

1
  • While this is true as well, this is not really the most important reason.
    – Evert
    Sep 7, 2015 at 19:26
0

When, for example, a file is in C:/xampp/htdocs/projecName/originalDirectory, the ../ path starts from the parent directory where the file is written, which, in this case, is originalDirectory.

If, however, this file is included somewhere else outside of the originalDirectory, then the parent directory is no longer the same, so, the path will fail.

Conclusion: using ../ is not portable because it is relative to the current directory.

This is the reason why we use __DIR__ as a prefix. The __DIR__ will "hardwire" the absolute path, which in this example is C:/xampp/htdocs/projecName/originalDirectory.

Hence, regardless of where the file is called from, it will always start from originalDirectory because it has been set in stone as follows:

C:/xampp/htdocs/projecName/originalDirectory/../filebeingincluded.php .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.