PHP:对文件路径感到困惑,包括无法正常工作,我猜

PHP: confused about file paths, includes not working as they should, I guess

我的目录结构如下:

--root
  |--datasource
  |  '--datasource.php
  |
  |--public_html
     |  '--app
     |     |--view
     |     |--model
     |     |--core
     |        '--App.php
     |     |--controler
     |     |--init.php
     |
     |-----css
     |-----js
     |-----index.php

我的 init.php(来自 "app" 目录)包含在 index.php 中,它包含我的项目的所有其他重要内容:

app/init.php:

    require_once '../../datasource/datasource.php'; --> Not working
  //require_once '../datasource/datasource.php'; --> Working
    require_once 'app/core/App.php'; --> Working
    //FUN PART:
    require_once 'core/App.php'; --> Still working, wtf?

    echo getcwd(); //gives: /home/prbh0pr/public_html

问题:为什么我的'../../datasource/datasource.php'包含失败?更重要的是,为什么 'core/App.php 不会失败?

我试图解决这个问题,但无法弄清楚发生了什么。

index.php

    <?php

      require_once 'app/init.php';

      $app = new App;

    ?>

首先用 get_include_path()

检查你的路径

../../datasource/datasource.php 失败,因为您的当前目录是 public_html 并且您使用的是相对路径。

Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.

发现于documentation about ini.include-path

app/core/App.phpcore/App.php 正在工作,因为

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

在有关 include 的文档中找到

app/core/App.php 是在当前工作目录 (home/prbh0pr/public_html) 中找到的,core/App.php 是使用调用脚本自己的目录 (~/public_html/app) 找到的