简体   繁体   中英

PHP: Include library in files in different directories

My project tree:

在此处输入图片说明

The library LibB is used in both AJAX and fileA , so I have respectively, in each file:

include_once "../Lib/LibB.txt"
include_once "Lib/LibB.txt"

The problem is that LibB depends on LibA , and whether I put an include that works for AJAX or that works for fileA :

 include_once "../LibA.txt"
 include_once "LibA.txt"

Is there a code that will work for both files?

set the path starting with the current directory

So from LibB.txt, use

include __DIR__ . '../LibA.txt';

Ref: http://php.net/manual/en/language.constants.predefined.php

or, assuming this is a website, you could use $_SERVER['DOCUMENT_ROOT'] before the file name

include $_SERVER['DOCUMENT_ROOT'] . '/LibA.txt';

call all the libraries in one file and call that file before calling your classes classes

ex. new file lib_list_request.txt
file list_classes.txt
file interface.txt

Like this, you only need to call every file once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM