problems resolving include

I really don't understand the way that phpstorm deals with includes.  

I have a fairly simple directory structure, where all the files are either in the main directory or a subdirectory.  When I do an include from the main directory there is no problem:  For example,

File index.php:

include_once "include/objects.php";

(no warning)


but in a file in a subdirectory, I get warnings, e.g.

File include/objects.php

include_once 'obj/Date.php';

warning : Path obj not found.

In the project settings, I include the root directory as the content root.  The code works, and the debugger works perfectly.  What am I doing wrong?

1 comment
Comment actions Permalink

Hi Marc,

PhpStorm searches for included files relative to the current file unless full path is provided. To make 2nd include work in PhpStorm you have to provide more info about included file: this should work:

include_once '../obj/Date.php';


It works fine in PHP because it does additional checks (first it searches for included file relative to the current path and then checks other places). If your site/server is very busy and you have a lot of includes then this is one of the things that slowdowns PHP script execution and requires optimizations -- you will find a lot of articles about this.

One of the recommended ways that works particularly well if you have single-entry approach (e.g. everything gets served via index.php, where the actual routing (which page to show) is done -- that's how most of modern frameworks work) is to define some constant that will contain physical path to the project on a server, which you then use to build an include/require file name. In this way PHP will search for an include file in single location as full path is provided straight away. This also works OK in PhpStorm (it really depends on complexity of such constant definition -- if you will use too many constants to build such path PhpStorm may fail to calculate it). For example:

index.php

define('DIR_ROOT', dirname(__FILE__));
...
include DIR_ROOT . '/include/objects.php';


objects.php

include_once DIR_ROOT . '/obj/Date.php';
0

Please sign in to leave a comment.