防止 PhpStorm 中变量变量的错误报告 [英] Prevent error reporting for variable variable in PhpStorm

查看:73
本文介绍了防止 PhpStorm 中变量变量的错误报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码并且得到两个未定义"变量的错误报告

I have this code and get an error reported for the two "undefined" variables

$tables = [
            'foo',
            'bar',
            'baz'
        ];
foreach ($tables as $table) {
    $$table = $this->setUpTables($table, $prefix);
}
$all = $this->getBaz($foo,$bar); // those two are reported as undefined

是否可以告诉 PhpStorm 不报告此错误"?

Is it possible to tell PhpStorm to not report this "error"?

/** @var foo $foo */
/** @var bar $bar */
$all = $this->getBaz($foo,$bar);

推荐答案

我认为在这种情况下使用更简单的语言功能会胜出.PhpStorm 也应该可以轻松确定哪些变量在范围内.

Using simpler language features wins in this case, I think. PhpStorm should also have no trouble figuring out which variables are in scope.

$products        = $this->setUpTables('products', $prefix);
$excludeRules    = $this->setUpTables('excludeRules', $prefix);
$excludedSellers = $this->setUpTables('excludedSellers', $prefix);
$livePricing     = $this->setUpTables('livePricing', $prefix);

$all = $this->getProducts($products, $livePricing);

如果 PhpStorm 认为一个变量不在作用域之外,则可以在作用域内添加此声明.

If PhpStorm thinks a variable is out of scope when it is not, you can add this declaration within the scope.

/** @var variableName */

这篇关于防止 PhpStorm 中变量变量的错误报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆