Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

eclipse

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?

like image 334
NextThursday Avatar asked Aug 21 '17 13:08

NextThursday


1 Answers

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)

like image 67
NextThursday Avatar answered Oct 02 '22 09:10

NextThursday