Eclipse PDT:“<class> 无法解析为类型”对于默认的 php 类,如 DateTime、Exception 等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45798965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse PDT: "<class> cannot be resolved to a type" for default php classes like DateTime, Exception etc
提问by NextThursday
After some recent Eclipse updates and workspaces changes I found some problems with validation of my PHP code in PDT. After re-adding all external libraries for my projects every external class was resolvable, but for PHP base classes like "DateTime" or "Exception" I get an error/annotation "DateTime cannot be resolved to a type". PHP Validation Version is set to 7.1, but even lower settings create the error.
在最近的一些 Eclipse 更新和工作区更改之后,我发现在 PDT 中验证我的 PHP 代码时出现了一些问题。在为我的项目重新添加所有外部库后,每个外部类都是可解析的,但是对于像“DateTime”或“Exception”这样的 PHP 基类,我收到错误/注释“DateTime 无法解析为类型”。PHP 验证版本设置为 7.1,但更低的设置也会产生错误。
How can I fix this validation problem?
如何解决此验证问题?
回答by NextThursday
There are many possible situations here; mine was an quite old project having a broken buildpath configuration. However using the GUI I wasn't able to fix it.
这里有很多可能的情况;我的是一个相当古老的项目,其构建路径配置已损坏。但是使用 GUI 我无法修复它。
Adding the following line to the .buildpath file in the project folder while eclipse was closed and afterwards restarting eclipse solves the problem:
在 eclipse 关闭时将以下行添加到项目文件夹中的 .buildpath 文件中,然后重新启动 eclipse 即可解决问题:
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
However, additionally if the code uses namespaces, one needs to use \DateTime instead of DateTime, or alternatively, add a 'use DateTime' on top;
但是,另外,如果代码使用命名空间,则需要使用 \DateTime 而不是 DateTime,或者在顶部添加“use DateTime”;
(Solution found after digging deeper into comments of other problems using Eclipse PDT does not propose all php functionsand https://bugs.eclipse.org/bugs/show_bug.cgi?id=502184)
(使用Eclipse PDT深入挖掘其他问题的评论后发现的解决方案并未提出所有 php 函数和https://bugs.eclipse.org/bugs/show_bug.cgi?id=502184)
回答by David Toledo
If you don't have a file called .buildpathin the root folder of your project, just create it.
如果您的项目根文件夹中没有名为.buildpath的文件,只需创建它。
The content of the file should be something like:
文件的内容应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path=""/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath>
That's going to assign all your folders as "source folders".
这会将您的所有文件夹分配为“源文件夹”。
That works fine in Eclipse Oxygen.3a Release (4.7.3a)
这在Eclipse Oxygen.3a Release (4.7.3a) 中运行良好
回答by nguyentaijs
Mine is a little different but it worths mentioning here for the newcomer like me.
I just import project using Import
> Projects from Folder or Archive
. It's just a folder no more no less.
You have to right click on your imported folder > Configure
> Convert to PHP Project ...
The Error on Exception
is gone.
我的有点不同,但对于像我这样的新人来说,这里值得一提。
我只是使用Import
>导入项目Projects from Folder or Archive
。它只是一个文件夹,不多不少。
您必须右键单击导入的文件夹 > Configure
>Convert to PHP Project ...
错误Exception
消失了。
回答by Sam
I recently ran into a similar situation with a project whose code is on a network server and the server went down. When it was restored the mounts had changed so some of my external libraries could not be found. This is how I fixed it:
我最近在一个项目中遇到了类似的情况,该项目的代码位于网络服务器上并且服务器出现故障。当它恢复时,安装已经改变,所以我的一些外部库无法找到。这是我修复它的方式:
In my project properties->php->source paths->include path libraries tab I added the paths to the external code. Then I ran Build project on my project followed by Refresh. The warnings went away.
在我的项目属性-> php-> 源路径-> 包含路径库选项卡中,我添加了外部代码的路径。然后我在我的项目上运行 Build 项目,然后刷新。警告消失了。
回答by naskar
In my case, I'm using the composer, then, the eclipse has an option to update the build path:
就我而言,我使用的是 Composer,然后,eclipse 可以选择更新构建路径:
Composer -> Update Build Path
Composer -> 更新构建路径
回答by ForJaysSake
@NextThursday mentions using the global scope using '\' before the class name. This is only important if you code is already scoped inside a specific namespace.
@NextThursday 提到在类名之前使用 '\' 使用全局范围。仅当您的代码已在特定命名空间内限定范围时,这才重要。
回答by sanmai
I had a problem where vendored dependencies (from vendor
folder) were unrecognized for a new project. E.g. if I had in code:
我遇到了一个问题,即vendor
新项目无法识别供应商依赖项(来自文件夹)。例如,如果我有代码:
use \Psr\Log\LoggerAwareInterface
It'll be underlined red with a note that "the import ... cannot be resolved", and outside of imports these classes had "cannot be resolved to a type" with them.
它将带有红色下划线,并注明“导入...无法解析”,并且在导入之外,这些类“无法解析为类型”。
Yet I had .buildpath
in place, but if look closely you notice the problem:
然而我已经.buildpath
到位,但如果仔细观察你会发现问题:
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path="examples"/>
<buildpathentry kind="src" path="src"/>
<buildpathentry kind="src" path="tests"/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath>
That's right, there's no entry for vendor
folder. Adding missing entry:
没错,vendor
文件夹没有条目。添加缺少的条目:
<buildpathentry kind="src" path="tests"/>
+ <buildpathentry kind="src" path="vendor"/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
And then cleaning/building the project fixed the problem right away.
然后清理/构建项目立即解决了问题。