PHPStorm: undefined variables caused by include/require

kxc picture kxc · Mar 18, 2014 · Viewed 16.2k times · Source

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?

Answer

Pawel Hawro picture Pawel Hawro · Jun 22, 2019

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.