Emm
Emm

Reputation: 23

PHP set_include_path

I have a MainClass.php file with other .php files in the same directory. I noticed that I can include other .php files inside this file by using:

require "file.php";

and it seems to load them even if I don't specify the full path to the script. Will this work on any server setup? Or do I have to manually add the full path or change the include path:

set_include_path(dirname(__FILE__));

before the require statements

?

Upvotes: 1

Views: 1294

Answers (1)

Phil
Phil

Reputation: 164723

By default, PHP's include path includes .. This represents the current working directory (CWD) which is why your includes are working.

I think it best to play it safe and use an explicit include path or absolute paths wherever possible as indicated in your question.

A popular method is

set_include_path(implode(PATH_SEPARATOR, array(
    $applicationIncludePath,
    get_include_path()
));

Upvotes: 3

Related Questions