Why do some frameworks use longer syntax?

It seems that the following two styles of code 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 many third-party codes. Is there a reason to prefer the second form?

+4
source share
2 answers

__DIR__is a magic constant relative to the current script file. The point ., however, refers to the current working directory, which can be changed, for example chdir().

+4
source

From PHP.NET :

__DIR__

. include, . dirname(__FILE__). , .

, :

__DIR__ PHP . , PHP, , , , , __DIR__. , , , , .

+3
source

Source: https://habr.com/ru/post/1658140/


All Articles