0

My Project structure looks something like this:

enter image description here

In my index.php I include the content from abilities.php with

<?php
    if (isset($_GET['site'])) {
        switch ($_GET['site']) {
            ...
            case 'abilities':
                include("./content/abilities.php");
                break;
            ...
        }
    }
?>

In abilities.php I include my Class Ability.php via

require_once ("./model/Ability.php");

which works perfect in the browser, but PhpStorm says it cannot find the class:

enter image description here

that's most likely because it searches for a model directory with the Class Ability.php in the content directory which isn't there.

So my question is how can I tell PhpStorm to start looking for files as if my abilities.php would be in the root directory like index.php?

Like setting a alternate path for abilities.php to start looking for files or something similar.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
TheDoctor
  • 2,362
  • 4
  • 22
  • 39
  • But .. your `require_once()` starts with `./` which tells IDE to "find the file relative to the **current** file/folder". Based on your folder structure it's obviously incorrect. During runtime PHP cannot find it relative to the current folder ... so it starts looking trough all folders from `include_path` and finds it elsewhere (relative to the project root). *Have you considered using more correct paths?* **P.S.** In your particular case using `../` instead `./` *should* also resolve the path – LazyOne Aug 05 '17 at 23:27
  • If i use `../` instead of `./` it will resolve correctly in `PhpStorm` but then the browser cannot find the file because the content of `abilities.php` will be loaded into `index.php` and `require_once ("../model/Ability.php");` will be incorrect. – TheDoctor Aug 06 '17 at 12:55
  • And what to you mean with "using more correct paths" ? – TheDoctor Aug 06 '17 at 12:57
  • *"And what to you mean with "using more correct paths" ?"* The paths that has no 2nd interpretation (e.g. if autoloading from one path failed then 2nd, 3rd etc path will be attempted). The best way -- use absolute path -- e.g. use `__DIR__` and path relative to current file. For you it might be `require_once (__DIR__ . "../model/Ability.php");` – LazyOne Aug 06 '17 at 13:14

2 Answers2

1

Have you configured you project structure in PHPStorm? Find Project Settings, and you can check if one is or mark a directory as 'sources'. That would the the parent directory of those shown in the screenshot in your case.

enter image description here

The screenshot is for a Java project but you can tell IDEA the same way for PHP projects where to look for source files.

marekful
  • 14,986
  • 6
  • 37
  • 59
0

If you just add files to your PHPStorm project the program will pick up on them. However, I think maybe what you're really after (maybe without realizing it) is how to use namespace in PHP. It's like a programatic directory structure that your application can read. There are tons of tutorials on it along with the official PHP documentation.

Difster
  • 3,264
  • 2
  • 22
  • 32