Php 尝试访问null类型值上的数组偏移量

Php 尝试访问null类型值上的数组偏移量,php,php-7.4,Php,Php 7.4,从PHP7.1迁移到7.4。我们对一个API进行了大约500次功能测试,其中一些在迁移完成后由于错误而开始失败。这些测试以前到处都是通过的,现在到处都是失败的——不是所有的,只有39次 环境资料: PHP7.4 共接受 yii2 堆栈跟踪: ...\api\vendor\codeception\codeception\src\Codeception\Subscriber\ErrorHandler.php:83 ...\api\tests\functional\SomeFileHereCest

从PHP7.1迁移到7.4。我们对一个API进行了大约500次功能测试,其中一些在迁移完成后由于错误而开始失败。这些测试以前到处都是通过的,现在到处都是失败的——不是所有的,只有39次

环境资料:

  • PHP7.4
  • 共接受
  • yii2
堆栈跟踪:

...\api\vendor\codeception\codeception\src\Codeception\Subscriber\ErrorHandler.php:83
...\api\tests\functional\SomeFileHereCest.php:72
...\api\vendor\codeception\codeception\src\Codeception\Lib\Di.php:127
...\api\vendor\codeception\codeception\src\Codeception\Test\Cest.php:138
...\api\vendor\codeception\codeception\src\Codeception\Test\Cest.php:97
...\api\vendor\codeception\codeception\src\Codeception\Test\Cest.php:80
...\api\vendor\codeception\codeception\src\Codeception\Test\Test.php:88
... more stuff here, not important
由于
ErrorHandler.php:83
这只是捕获错误,让我们看看
somefileherest.php:72

// declaration of the apiPrefix variable in the class.
protected $apiPrefix;
//...

public function _before(FunctionalTester $I)
{
    $this->apiPrefix = $this->config['backend']['api_prefix']; // this is the line 72
    //... more similar stuff later
因此,
$this->config['backend']['api_prefix']
这是一个字符串(“v1”)


我不知道问题在哪里,也不知道该如何深入研究。有什么想法吗?

听起来您的变量没有设置

检查以下isset调用:

isset($this->config); 
isset($this->config['backend']);
isset($this->config['backend']['api_prefix']);
实际上,您可以在一个isset调用中检查多个var(
isset($x,$y,$z)
),但这将让您看到具体缺少哪个var(
双问号运算符
)(“
空合并运算符
”)以避免未设置 数组

这个单元测试给了我“成功”


首先检查
isset($this->config)
isset($this->config['backend'])
isset($this->config['backend']['api_prefix'])
好的,Idk为什么,但它没有在这个文件中加载配置-即使代码根本没有更改。因此,如果您从中创建一个答案,我可以接受它是一个好的答案,因为我可以通过在这里首先调用load-config函数来解决这个问题,谢谢!
class PhpTest extends TestCase
{
    public function test_php_74()
    {
        //Trying to access array offset on value of type null

        $this->assertSame('7.4.9', phpversion());

        $a = null;
        $this->assertTrue($a ?? true);
        $this->assertTrue($a['a'] ?? true);
        $this->assertTrue($a['a']['a'] ?? true);

        $a = [];
        $this->assertSame([], $a);
        $this->assertTrue($a['a'] ?? true);
        $this->assertTrue($a['a']['a'] ?? true);
    }
}