简体   繁体   English

Visual Studio Code 找不到 PHP function - 也许是“require_once”语法?

[英]Visual Studio Code can't find PHP function - maybe “require_once” syntax?

I would like to switch to Visual Studio Code for PHP development.我想切换到 Visual Studio Code 进行 PHP 开发。

All of my files start with require_once with this syntax我所有的文件都以require_once开头,语法是这样的

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并且调试不会启动,大概是因为 VSC 说要调试的文件存在问题,大致如下

Undefined function 'DumpGetAndPostParametersIfDebugging'.未定义 function 'DumpGetAndPostParametersIfDebugging'。

where that function is declared in one of my required files.其中 function 在我所需的文件之一中声明。

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.奇怪的是, require_once语句没有报告任何问题,但我想不出没有找到 function 的其他解释。

The code works just fine with PHPstorm, I am testing on localhost - any ideas as to what I am doing wrongly?该代码在 PHPstorm 上运行得很好,我正在localhost上进行测试 - 关于我做错了什么的任何想法?


[Update] the error is happenig with every function which is declared in a required file. [更新] 在所需文件中声明的每个 function 都会发生错误。 Here's what leads to the first example.这是导致第一个示例的原因。

in RequireCommonStuff.php , I just have more require_once s:RequireCommonStuff.php中,我只有更多require_once

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它的内容是(注意:不涉及任何课程),header 评论被剥离

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

not sure about it but you can install and test PHP Intelephense instead of Intellisense.不确定,但您可以安装和测试PHP Intelephense而不是 Intellisense。 also check if php path is added in vscode " php.validate.executablePath ".还要检查 php 路径是否添加到 vscode“ php.validate.executablePath ”中。

Have you tried using the DIRECTORY_SEPARATOR constant instead of /您是否尝试过使用DIRECTORY_SEPARATOR常量而不是/

$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.刚刚遇到同样的问题并想通了:如果您还没有这样做,您应该使用“文件”菜单中的“打开文件夹”并导航到 PHP 脚本所在的位置。 That way VSC apparently finds both scripts in its working directory and does not show the error anymore.这样,VSC 显然会在其工作目录中找到这两个脚本,并且不再显示错误。

Side note: I am running VSC 1.55.2 and "PHP Intelephense", in case just opening the folder does not suffice for anybody.旁注:我正在运行 VSC 1.55.2 和“PHP Intelephense”,以防仅打开文件夹对任何人都不够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM