簡體   English   中英

包含一個包含另一個文件的文件

[英]Including a file that includes another file

資料夾結構

    • 公司/
      • db.php
      • session.php
      • db.php
      • init.php
    • lib /
      • 枝條/
  • init.php

login.php文件

<?php
include 'inc/init.php';

init.php

<?php
include 'session.php';

$config = include 'config.php';
include 'db.php';

require_once '/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

我很困惑的是Twig庫被包含在內。 我不需要備份文件夾../嗎? 但這給了我一個錯誤,我根本不明白為什么。

您正在為Twig使用絕對路徑,這就是為什么包含它的原因。 如果要使用相對路徑從init.php文件中包含它,則應如下所示:

require_once './lib/Twig/Autoloader.php';

PHP的include語句所做的是,它將獲取您包含的文件並將所有文件轉儲到包含文件中。 因此,如果您在根文件夾的init.php中包含一個文件,則所有包含的都是相對於根的。

要了解這一點。 在您的包含文件中添加一些語句,例如printf("I am in init.db, the current folder is %s", getcwd()); 這將有助於您了解包含文件時的當前目錄,並且可以相應地修改包含文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM