Search code examples
phpinclude-path

Why do some frameworks use a longer include syntax?


It seems the following two code styles do the same job:

require_once './foo.php';
require_once './../bar.php';

require_once __DIR__.'/foo.php';
require_once __DIR__.'/../bar.php';

Clearly, the first form is shorter and cleaner. However, I see the second form in a lot of third party codes. Is there any reason to prefer the second form?


Solution

  • __DIR__ is a magic constant, relative to the current script's file. The dot ., however, is relative to the current working directory, that could have been altered by chdir(), for instance.