Visual Studio Code can't find PHP function - maybe "require_once" syntax?

Viewed 1708

I would like to switch to Visual Studio Code for PHP development.

All of my files start with require_once with this syntax

require_once(__DIR__ .  DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'common'  . DIRECTORY_SEPARATOR . 'api'  . DIRECTORY_SEPARATOR . 'RequireCommonStuff.php');

and debugging won't launch, presumably because VSC says that there are problems with the file to be debugged, along the lines of

Undefined function 'DumpGetAndPostParametersIfDebugging'.

where that function is declared in one of my required files.

Strangely, there are no problems reported for the require_once statements, but I can think of no other explanation for the function not being found.

The code works just fine with PHPstorm, I am testing on localhost - any ideas as to what I am doing wrongly?


[Update] the error is happenig with every function which is declared in a required file. Here's what leads to the first example.

in RequireCommonStuff.php, I just have more require_onces :

require_once(__DIR__ . DIRECTORY_SEPARATOR . 'constants.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'third_party' . DIRECTORY_SEPARATOR .'ChromePhp.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'errorHandling.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'trace.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'parseAndValidateUrlGetParameters.php');

The last of those is causing the error I discuss (although all of them cause erros). It's contents are (note: no classes involved), header comment stripped

function ParseAndValidateUrlGetParameters($url, 
                                         $inputType, 
                                         $filter, 
                                         $debug, 
                                         $reportIndicent=true, 
                                         $allFieldsMandatory=true)
{
3 Answers

not sure about it but you can install and test PHP Intelephense instead of Intellisense. also check if php path is added in vscode "php.validate.executablePath".

Have you tried using the DIRECTORY_SEPARATOR constant instead of /

$slash = DIRECTORY_SEPARATOR;
require_once(__DIR__ . "{$slash}..{$slash}..{$slash}common{$slash}api{$slash}RequireCommonStuff.php");

Just had the same problem and figured it out: If you haven't done so you should use "Open Folder" from the "File" menu and navigate to where your PHP scripts are located. That way VSC apparently finds both scripts in its working directory and does not show the error anymore.

Side note: I am running VSC 1.55.2 and "PHP Intelephense", in case just opening the folder does not suffice for anybody.

Related