【问题标题】:calling include from an included file从包含的文件中调用包含
【发布时间】:2011-03-21 00:40:42
【问题描述】:

所以, 检查这个目录结构

  • /include_one.php
  • /include_two.php
  • /directory/main_file.php

假设我在 /directory/main_file.php 中,我在 include_one.php 中调用 include('../include_one.php');,以包含 include_two.php时间>。我需要打电话给include('include_two.php);include('../include_two.php');吗?

所以我的问题是:包含文件时,“相对包含路径”是否转移到包含文件,还是保留在主包含文件中?

我知道最好的选择是拥有一个包含 root_path 的 config.php,但是在这个阶段这是不可能的。


更新:
所以,我不确定谁是对的,因为这是我的测试

目录结构

/include.php
/start/start.php
/folder1/includeone.php
/folder1/folder2/includetwo.php

这是每个文件的内容

start.php

<?php 
  echo 'including ../include.php<br />';
  include('../include.php');
?>

include.php

<?php 
  echo 'including folder1/includeone.php<br />';
  include('folder1/includeone.php');
?>

includeone.php

<?php 
  echo 'including folder2/includetwo.php<br />';
  include('folder2/includetwo.php');
?>

includetwo.php

<?php 
  echo 'done<br />';
?>

输出是

包括 ../include.php
包括文件夹 1/includeone.php
包括文件夹 2/includetwo.php
完成

【问题讨论】:

  • 我的更新仍然没有答案...
  • 嗨。我相信这取决于运行代码的系统。我目前正在从事一个已有 4 年历史的项目,该项目跨不同的操作系统和设置运行。不知何故,每次我克隆代码时,我都必须重新配置所有内容,而我的同事每次将代码克隆到他的机器上时都可以简单地运行它。我目前正在寻找一种方法,让 PHP 在每次从文件更改到文件时都会转移“相对包含路径”。

标签: php include-path


【解决方案1】:

“相对包含路径”没有转移到包含文件...这意味着使用相对路径通常会以失败告终。

我几乎一直使用的更好的解决方案是始终使用绝对路径——您可以使用__DIR__ 混合使用相对路径和绝对路径,以获取包含写入文件的目录。


例如,在include_one.php 中,您会使用:

require_once __DIR__ . '/include_two.php';

要包含与include_one.php 位于同一目录中的include_two.php 文件。


而且,在main_file.php 中,您可以使用:

require_once __DIR__ . '/../include_one.php';

包含一个目录上的include_one.php 文件。


这样,无论从哪个文件调用它们,您的包含都将起作用。

【讨论】:

  • 为了节省一些空间,__DIR__ 等价于dirname(__FILE__)
【解决方案2】:

包含路径相对于包含链中的第一个文件。

确保正确包含路径的一个好方法是始终从文档根目录包含。

这样做是这样的:

include $_SERVER['DOCUMENT_ROOT'] . '/folder1/folder2/includetwo.php';

【讨论】:

    【解决方案3】:

    我正在使用此代码:

    if(!isset($TO_HOME_DIR)) $TO_HOME_DIR="../"; 
    

    我包含一个文件:

    include_once($TO_HOME_DIR."common/include_one.php");
    

    对于if(!isset($TO_HOME_DIR)),将文件包含到包含文件到包含文件到包含的程度并不重要.....只使用第一个文件的 - 和主文件的 - $TO_HOME_DIR 声明。

    这种方法的第二个好处是,如果你改变文件目录,你只需要改变一行代码; $TO_HOME_DIR 声明。 :)

    【讨论】:

      【解决方案4】:

      PHP 中包含如何工作的两个演示

      我是 2010 年作者在他的问题中写道:

      所以,我不确定谁是对的,因为这是我的测试......

      还有comment:

      我的更新仍然没有答案...

      所以我建议我进行测试以了解 PHP 如何包含工作。版本 5 和 7 的结果相同。

      测试 1

      文件和目录:

      1. /index.php
          <?php
          include 'subdirectory/index.php';
      
      1. /subdirectory/index.php
          <?php
          include 'test.php';
      
      1. /subdirectory/test.php
          <?php
          echo __FILE__;
      

      输出:

      /子目录/test.php

      此测试运行良好,因为每个包含只有一个候选者

      更新问题的测试也满足此要求。这也是它运作良好的原因。

      测试 2

      让我们将test.php 复制到主根目录。所以新的项目结构如下:

      /index.php
      /test.php
      /subdirectory/index.php
      /subdirectory/test.php
      

      现在输出变得不同了::

      /test.php

      第二次测试还有一个结果,因为指令include 'test.php';有两个候选项,PHP自己选择包含哪个文件。

      这就是为什么accepted answer 的作者建议的做法可以使项目免于意外。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-13
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2010-10-19
        • 2011-08-02
        • 1970-01-01
        相关资源
        最近更新 更多