PHPStorm: undefined variables caused by include/require
Solution 1
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.
Solution 2
As hakre pointed out, this warning could be a sign of bad code smell. However, you can disable PHPStorm's inspection of undefined variables if there is a prior include
or require
call.
Uncheck the radio button in Settings/Editor/Inspections/PHP/Undefined variable called "Ignore 'include' and 'require' statements".
If you don't want to disable this inspection, you can disable it for a single statement by using PHPStorm's @noinspection annotation. Example code:
require $path; // $some_variable is defined in that file
/** @noinspection PhpUndefinedVariableInspection */
$config["DBSettings"] = [
"server" => $some_variable
];
Solution 3
Enable "Search for variable's defination outside the current file"
Solution 4
Yes it is. However, this should hint you to another issue your code most likely has (and which Phpstorm can not warn specifically about): You exploit the usage of global variables too much in addition to how you use include
and require
.
You can easily improve here by modularizing your code more, e.g. by making use of functions with parameters. Additionally you can consider (but you don't need to in PHP) to start writing classes and program with objects.
For PHPStorm itself, you can exclude single or multiple statements with annotations to disable the warning in certain places. However if you have many "false positives", this is cumbersome. This btw. works with any inspection warning.
See as well:
- Suppressing Inspections (PHPStorm Webhelp)
- phpstorm warning php variable might not have been defined
Solution 5
By using global $db
not just at the top of my function, but also atop of other files, phpstorm somehow figures out where to find the declaration and the /** @var
statement of that var.
Comments
-
kxc almost 2 years
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 fileb.php
, which require the filea.php
. When I typeecho $name
in fileb.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 fileb.php
and the IDE doesn't highlight it.Can PHPStorm understand which variables are set in included/required files and which ones are not?
-
hizmarck over 4 yearsYeah! you are correct! I like this solution because don't modify the code base in my share repository, I mean code with other annotation that other programmers don't matter.
-
FrankyBoy over 3 yearsSo what would be the actual proper way to fix this? Functions do not seem helpful in this regard at all.
-
hakre over 3 years@FrankyBoy: Please see the follow-up answer stackoverflow.com/a/35147818/367456 that shows how to control the inspection warnings in Phpstorm more dedicated. Also meta files may help or \@var definitions. Perhaps also defining as global variables.
-
FrankyBoy over 3 yearsboth of these seem like workaround to me to just get rid of the pesky warning, without addressing the actual underlying problem/antipattern. I'm asking on alternatives for the antipattern, not how to ignore warnings.
-
hakre over 3 years@FrankyBoy Please describe a bit more what is the antipattern in your case, then I might be able to give suggestions in that direction. It's hard to make suggestions without knowing a bit more context.
-
CodeConnoisseur over 3 years@Pawel Hawro This worked for me. I was getting frustrated because I instantiated an object in an init.php, then
include
ed the init.php in a login.php and in my login.php the object/class I instantiated was error-ing. But adding the doc-blocks as you explained stopped the errors from showing. (I agree that suppressing errors is NOT the way to go) -
fustaki over 2 yearsNice, and at the same time PhPStorm will help you with lint feedback and autocomplete