Php& #39;s include_path for包含以./开头的内容


php's include_path for includes starting with ./

在我的PHP框架中,我想使用另一个PHP框架的几个函数。另一个框架只有一个门户脚本(index.php)。从那里它做一切(引导,调用控制器和动作等)。另一个框架包含所有以。/

开头的文件。

其他框架的index.php:

include './inc/bootstrap.php';
在bootstrap.php:

include './inc/configs.php';
include './inc/database.php';

等等

所以看起来所有的include都是相对于index.php所在的文件夹

是否有任何方法来设置环境,以便我可以从另一个文件夹(在我的框架内的某个地方,所以一个完全不同的文件夹,而不是门户脚本)引导框架?

include_path包含.,我也尝试过在include_path中使用其他框架的文件夹,但这并没有改变任何东西。

我猜它是。/include,但我不能改变那些(另一个框架不是我的框架的一部分,将在某个时间更新)。是否有办法绕过它们(或者我做错了)?

路径以。或者忽略include_path,因为它们是相对于工作目录的。

所以唯一的方法是使用PHP函数chdir: 来改变工作目录

在你的框架中:

chdir('/path/of/the/other/framework'); // change the working directory
require '/path/of/the/other/framework/bootstrap.php'; 
// optionally you can reset the working directory
chdir(dirname(__file__));

当你去包含其他框架的引导文件时,你需要首先将chdir()放入该目录,然后你可以包含它,并且所有后续包含它将做的将正确地相对于引导文件

您应该能够使用set_include_path( $path_to_include_files )执行此操作。如果您仍然有问题,这可能意味着在您的脚本中有另一个地方正在设置include_path为另一个值。

文件是按包含路径,在本例中为目录结构如下:


│- index.php
│- t1
│,,,,,│——
│,,,,,└─ b
t2
,,,,| - b
,,,,└─ c

<?php
set_include_path('t1' . PATH_SEPARATOR . 't2');
include 'a';  // includes from T1
include 'b';  // includes from T1
include 'c';  // includes from T2
?>

请注意包含路径影响只包含/require函数

<?php
var_dump(file_exists('a'));  // false
var_dump(fopen('b', 'r'));  // file not found
?>

相关文章: