Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStorm: undefined variables caused by include/require

PHPStorm showed that all the variables from other files, both required and included, are undefined. I found this solution here, but after I disabled that option Ignore 'include' and 'require' statements, the IDE ignored all undefined variables.

For example, I have a file a.php with content $name = 'Bob', and file b.php, which require the file a.php. When I type echo $name in file b.php it works as expected and displays 'Bob'. The IDE, however, highlights the variable $name claiming it's undefined. If I disable that option 'Undefined variable' - Ignore 'include' and 'require' statements, the IDE stops highlighting it.

With that option I can now write any variables, for example $sdadasdasdas in file b.php and the IDE doesn't highlight it.

Can PHPStorm understand which variables are set in included/required files and which ones are not?

like image 971
kxc Avatar asked Mar 18 '14 10:03

kxc


1 Answers

All above answers removes or suppress warnings which imho is not good solution.

Best solution is to add header with doc block with used variables.

Example:

<?php /**  * @var string $name  * @var FormData $form  */ ?> 

This will not only prevent from showing "Undefined variable" warning, but also document your code and makes autocomplete working as expected.

like image 52
Pawel Hawro Avatar answered Sep 22 '22 14:09

Pawel Hawro