谁能说出 php 中 include 和 require 与魔法常量的关系是什么?

Can anyone tell what is the relation of include and require with magic constants in php?

我正在学习 php.net and found the line An exception to this rule are magic constants which are evaluated by the parser before the include occurs. on 7 paragraph of that page 上的 include 和 require 构造,但不明白魔法常量与 php 中的 include 和 require 有什么关系。

谁能用通俗易懂的话告诉我?

对我的问题打负分可以,但请务必回答,我想知道和学习,打负分没关系。

在解析您的代码之前,解析器会解析所有 include 和 require,因此它可以像解析一个脚本一样解析它们。但是,在它解决这些问题之前,任何魔术常量,如 __DIR__ 都将被解决。

一个例子:

假设您有两个文件:

file1.php

<?php
require __DIR__ . '/file2.php';

echo 'Hello ' . $a;

file2.php

<?php
$a = 'World';

如你所见,那里有一个神奇的常量:__DIR__。该常量将 return 是我写入它的文件的绝对路径。所以解析首先解析:

<?php
require '/the-current-folder/file2.php';

然后它实际上包括任何 includerequire 所以它得到:

<?php
$a = 'World';

echo 'Hello ' . $a;

然后它解析脚本:

Hello World