【问题标题】:Including file from various folders包括来自各种文件夹的文件
【发布时间】:2016-06-15 20:05:05
【问题描述】:

这让我很头疼。我已经阅读了多个问题,但没有一个对我有帮助。

所以我需要包含来自不同文件夹的 php 文件。这是我的结构的样子。我正在使用 WAMP。

/C
    /wamp
      /www
       /project1
        /main
         /backend
          -class.common.php
          -inc.config.php
            /providers
             /google
              -google.functions.php

从上面的结构可以清楚地看出,class.common.php 在文件夹 backend 下(在文件夹 main 下)。

现在,我正在尝试使用此代码将 class.common.php 包含在 google.functions.php 中:-

include '../../class.common.php';

但是,这似乎不起作用。我什至尝试过使用__DIR__ 常量。没运气。实现我的结果最可靠的方法是什么?

PHP 错误日志。

    [15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(../../_inc_config.php): failed to open stream: No such file or directory in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(): Failed opening '../../_inc_config.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Fatal error:  Cannot redeclare class common in C:\wamp\www\yele\main\backend\class.common.php on line 5

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:3

【问题讨论】:

  • 将您的 includes 更改为 include_once

标签: php


【解决方案1】:

我不确定我是否正确地阅读了您的图表,但鉴于以下情况:

如果google.functions.php 的完整路径是

C:/wamp/www/project1/main/backend/providers/google/google.functions.php

class.common.php 的完整路径是

C:/wamp/www/project1/main/backend/class.common.php

要包含该文件的相对位置,那么__DIR__ 应该可以工作

<?php
include_once __DIR__.'/../../class.common.php';

更新

从您的错误日志来看,您有几个问题。

  • C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php 中发生了包含失败,它调用了一个相对路径包含include(../../_inc_config.php),它会根据包含脚本的文件而变化。所以这对供应商来说很奇怪,但您可以尝试将行更改为 include(__DIR__.'/../../_inc_config.php') 以强制它包含相对于实际包含文件的内容,这恰好是我对您问题的原始答案。
  • 在重新声明common 类时出现致命错误,因此请使用include_oncerequire_once

【讨论】:

  • 没用。我注意到的另一件事是,如果我将文件包含在测试文件中(除了包含文件之外没有其他代码),那么它可以正常工作。但是当我将它包含在一个有一些好的代码的文件中时,它就不起作用了。
  • 所以你说包含工作,但它与其他代码冲突?那么这是一个不同的问题。确保您使用的是error reporting
  • 我正在使用错误报告。我用最近的日志更新了我的问题。
  • 我认为这可以解决。但是,出现了一个新错误Cannot redeclare class。我想我需要一些时间来修复它。谢谢。
【解决方案2】:

首先,获取真实/物理路径可能更有用

include realpath('/../../class.common.php');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2019-05-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多