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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With