从php的任何地方都包含相同的指向文件吗?


Identical include from anywhere in php for a directing file?

我正在对项目进行大量重构,因此它实际上可以具有开发分支。现在,所有包含的内容都经过硬编码,确实很乱。许多包含将指向相同的文件,但是包含的外观与每个文件都非常不同(并且,对于开发站点而言,必须有所不同,暂时还需要将相同的文件复制到不同的目录中) 。

我已经看过以下答案:StackOverflow:当前PHP脚本相对于文档根目录的路径,并尝试实现

getpath.php与

1
2
3
<?php
define("DIR_BASE", dirname( __FILE__)."/");
define("SERV_CONFIGS", DIR_BASE."server/specific/config.php");

给我一个不错的起点,它可以适应整个位置(以及一些特定于环境的位置变量),这样我就可以更改每个

实例

1
2
3
include ("../../../include/foo.bar");
include ("http://example.com/something/else.php");
include ("../../../server/specific/config.php");

1
2
3
include ("../include/foo.bar");
include ("../something/else.php");
include ("http://server/specific/config.php");

1
2
3
include ("http://example.com/include/foo.bar");
include ("/something/else.php");
include ("/server/specific/config.php");

等全部变成

1
2
3
include (DIR_BASE ."include/foo.bar");
include (DIR_BASE ."something/else.php");
include (SERV_CONFIGS);

这样,我可以将这两行复制粘贴到数百个文件中并完成。 (这将不仅仅是三个文件名)。

但是,我突然意识到,要包含我的getpath.php,每个页面仍然需要一定的"唯一雪花",每个页面都指向该文件并指向getpath.php以包含它。 >

如何在不使每个页面具有包含自己的雪花版本的include(" getpath.php ")的情况下包含getpath.php?


您可以在需要包含getpath.php的任何文件中使用$ _SERVER ['DOCUMENT_ROOT']:

1
include $_SERVER['DOCUMENT_ROOT'] . '/path/to/getpath.php';

这避免了为不同的文件编写特定的路径,例如:

1
include '../../../../../getpath.php';