PHP require_once and paths

47 views Asked by At

I inherited a PHP app that's been developed in Eclipse. While familiarizing myself with the code I noticed something odd. A little background. There are 2 code projects that share Common code. Each of these 3 are at the same directory level. The two projects have a softlink that points to common code. While reviewing various files, I noticed two different methods of including a file.

require_once 'common/utils/DatabaseClasses.php';
ahd
require_once '../common/utils/DatabaseClasses.php';

Is there a difference? Advantages/disadvantages? Eclipse will autocomplete the first method. Aside from that, I don't notice a difference. Also, there are no namespaces used in this app. Not sure if that's relevant here or not.

This is how the dirs/links are organized.

Project1
    |------------- ../Common
Project2
    |------------- ../Common
Common
1

There are 1 answers

0
Kaddath On

From the manual there is a difference if you have set a include_path in your PHP configuration (php.ini file):

require_once 'common/utils/DatabaseClasses.php'; will try first to find the file in the include_path if it is set, then in the script directory and last in the current working directory.

If an absolute or relative path is used like in require_once '../common/utils/DatabaseClasses.php';, the include_path search will be ignored.

So because the "generic" common folder is out of the projects folders, there's a strong chance that the include_path is set to its value, or else I don't see how these files would be included without pointing explicitly to it from the parent folder or from the root (as pointed in the comments). So I guess the first version is designed to point to the "generic" folder while the second points to the project's one. This is just a supposition, you have to check your configuration