0

I'm using VS code to write some PHP and have some functions defined in a file included with auto_prepend_file. This works great at runtime, however, VS Code can't resolve the functions so I have errors reported for every file in which I use the functions.

Is there some way to help VSC to resolve these functions?

  • Does this answer your question? [PHP Intellisense in Visual Studio Code](https://stackoverflow.com/questions/52013743/php-intellisense-in-visual-studio-code) – Dharman Dec 31 '19 at 00:14

2 Answers2

0

No, VSC has no understanding of PHP's auto_prepend_file configurations. No IDE will, actually. Additionally, this is one of the many reasons why relying on auto_prepend_file and other PHP-specific configurations is a bad idea. You simply must be completely aware and at the mercy of PHP's configuration in order to understand how your code works.

Instead, use autoloaders for loading classes and make sure you include your dependencies in code and not in PHP's runtime configuration.

P.S.: IDE's like PhpStorm can actually resolve functions by following includes in code, which is further reason to resolve dependencies through code rather than configuration.

Sherif
  • 11,786
  • 3
  • 32
  • 57
  • I am not sure about PHP Storm, because I don't have it, but the rest of your answer is technically correct, but not fully. Whether the file is included in the code or via INI settings, VS Code will not pick it up. – Dharman Dec 30 '19 at 23:59
  • Sure, but I did not imply anywhere that VS Code would or could pick up the include. I'm simply stating that the person reading the code can better understand the dependencies if they are included in code. The idea of going out of your way to read through configuration files in order to understand project dependencies (specifically configuration files outside of the project itself such as php.ini) is a cognitive strain and derailing action on the part of the programmer. It is not desirable in any project. – Sherif Dec 31 '19 at 00:02
  • *"Instead, use autoloaders for loading classes and make sure you include your dependencies in code"* <- This is a good suggestion, but doesn't answer the question. Neither autoloader nor include nor auto_prepend_file will help. If the function is declared in a separate file then VS code is unaware of it. – Dharman Dec 31 '19 at 00:07
  • The question is clearly answered by the first two letters in my answer. Further reading is ancillary to good practices and advice in any project or applying to any IDE in general. This is for the benefit of the programmer and not the IDE as stated earlier. – Sherif Dec 31 '19 at 00:09
0

There was an answer, which is now removed that suggested to install an extension. There are at least two intellisense extension available, both of which will index your workspace and provide intellisense suggestions, whether you include these files or not. These extensions can't and don't rely on the flow of your code. Whether you include the files via includes or via INI directive it should have no effect on the code suggestions.

As far as I aware VS Code has very basic intellisense support for PHP out of the box, so it can only suggest elements from the same file. It will not index the whole workspace.

Dharman
  • 30,962
  • 25
  • 85
  • 135