php 的 include_path 包含以 ./ 开头的内容

发布于 2024-11-01 03:04:33 字数 600 浏览 3 评论 0原文

在我的 PHP 框架中,我想使用另一个 PHP 框架的几个功能。另一个框架只有一个门户脚本 (index.php)。从那里它执行所有操作(引导、调用控制器和操作等)。另一个框架包含以 ./ 开头的所有文件

另一个框架的index.php:

include './inc/bootstrap.php';

在bootstrap.php中:

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

等等等等

所以看起来所有包含内容都相对于index.php所在的文件夹。

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

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

我猜这是 ./ 包含,但我无法更改它们(另一个框架不是我的框架的一部分,并且会在某个时候更新)。有没有办法绕过它们(或者我做错了)?

In my PHP framework, I want to use several functions of another PHP framework. That other framework has only one portal script (index.php). From there it does everything (bootstrap, call controllers and actions etc). The other framework includes all its files starting with ./

The other framework's index.php:

include './inc/bootstrap.php';

In bootstrap.php:

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

etc etc

So it looks like all the includes are relative to the folder index.php is in.

Is there any way to set up the environment so I can bootstrap the framework from another folder (somewhere within my framework, so a completely different folder and not the portal script)?

include_path includes . and I've tried it with the other framework's folder in the include_path as well but that didn't change anything.

I'm guessing it's the ./ includes, but I can't change those (the other framework isn't part of my framework and will be updated some time). Is there a way around them (or am I doing it plain wrong)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福还没到 2024-11-08 03:04:33

以 . 开头的路径。或 / 忽略 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__));

path starting with . or / ignore the include_path because they are relative to the working directory.

So the only way is to change the working directory using the PHP function chdir:

In your framework:

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__));
晨敛清荷 2024-11-08 03:04:33

当您要包含其他框架的引导程序文件时,您需要 chdir() 首先进入该目录,然后您可以包含它,并且它将执行的所有后续包含都将正确地相对于引导文件。

When you go to include the other framework's bootstrap file, you'll need to chdir() into that directory first, then you can include it, and all the subsequent includes it will do will be properly relative to the bootstrap file.

春庭雪 2024-11-08 03:04:33

您应该可以使用 set_include_path( $path_to_include_files ) 来完成此操作。如果您仍然遇到问题,则可能意味着脚本中的另一个位置将 include_path 设置为另一个值。

You should be able to do this with set_include_path( $path_to_include_files ). If you still have problems it might mean that there is another place in your script that is setting the include_path to another value.

青衫负雪 2024-11-08 03:04:33

文件按以下顺序包含
包含路径,在本例中,
目录结构如下:

.
│— index.php
│— t1
│     │— a
│     └─ 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

?>

请注意,包含路径会影响
仅包含/需要函数。

<?php

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

?>

The files are included in order of the
inclusion path, in this example, the
directory structure is as follows:

.
│— index.php
│— t1
│     │— a
│     └─ 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

?>

Note that the include path affects
only include/require functions.

<?php

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

?>

source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文