5

[Solution] The "Undefined function" message was actually coming from SonarLint. I added type hinting to the function, and SonarLint no longer complains.


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)
{
5
  • 1
    Does this help? code.visualstudio.com/docs/languages/php Commented Jul 31, 2020 at 13:06
  • Alas, no. But, upvoted for a useful link :-) Commented Jul 31, 2020 at 14:21
  • No one else here uses VSC for PHP development? Commented Aug 8, 2020 at 7:02
  • 1
    Although I'm not suggesting to change your coding style, could you try temporarily switching to using the literal require_once __DIR__ . '/constants.php' format instead? Just in case VSC is having trouble parsing that out. It'd be weird, but it is good to rule out. Commented Aug 11, 2020 at 21:21
  • You, sir, are a genius!! Post that as an answer and I will accept it & award the bounty. Commented Aug 12, 2020 at 8:02

3 Answers 3

3

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tips. Yes, the path is set in the folder's JSON config. Switching InteliSense did not help :-(
3
+25

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");

3 Comments

Sorry, but that didn't hel[P :-( require_once(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'RequireCommonStuff.php'); still gives me the error. No problem with the REQUIRE_ONCE, but Undefined function for functions in that file
Can you share in the issue description the content of that file that you are requiring?
Done. I hope that it helps. This is all fine in PHPstorm, just not in VSC
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.