NextThursday
NextThursday

Reputation: 2432

Eclipse PDT: "<class> cannot be resolved to a type" for default php classes like DateTime, Exception etc

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.

How can I fix this validation problem?

Upvotes: 7

Views: 20407

Answers (9)

user1283182
user1283182

Reputation: 131

Sometimes we didn't notice a missing ";" or "}" that could lead to this problem. So please check if there's any new introduced grammar error if the problem has never happened in the file before.

Upvotes: 0

Orkan
Orkan

Reputation: 96

I have recently run into this problem by re-opening a working project the next day in Eclipse 2021-06...

What I did was disable all Validators, remove all Errors from the Problems view, and re-enabled Validators again. Then Project > Validate fixed it!

Edit: I saw that it created new file: .settings/org.eclipse.wst.validation.prefs

Upvotes: 0

rafaelnaskar
rafaelnaskar

Reputation: 783

In my case, I'm using the composer, then, the eclipse has an option to update the build path:

Composer -> Update Build Path

Upvotes: 1

sanmai
sanmai

Reputation: 30911

I had a problem where vendored dependencies (from vendor folder) were unrecognized for a new project. E.g. if I had in code:

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:

<?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:

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

Upvotes: 0

user8972079
user8972079

Reputation:

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.

Upvotes: 1

nguyentaijs
nguyentaijs

Reputation: 150

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.

Upvotes: 2

David Toledo
David Toledo

Reputation: 494

If you don't have a file called .buildpath in the root folder of your project, just create it.

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)

Upvotes: 6

NextThursday
NextThursday

Reputation: 2432

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.

Adding the following line to the .buildpath file in the project folder while eclipse was closed and afterwards restarting eclipse solves the problem:

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

(Solution found after digging deeper into comments of other problems using Eclipse PDT does not propose all php functions and https://bugs.eclipse.org/bugs/show_bug.cgi?id=502184)

Upvotes: 15

ForJaysSake
ForJaysSake

Reputation: 48

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

Upvotes: 2

Related Questions