亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試訪問 null 類型值的數組偏移量

嘗試訪問 null 類型值的數組偏移量

PHP
慕雪6442864 2022-06-17 10:28:18
從 php 7.1 遷移到 7.4。我們對一個 API 進行了大約 500 個功能測試,其中一些在遷移完成后開始失敗并出現錯誤。這些測試以前到處都是通過,現在到處都失敗了——不是全部,只有 39 個。環境信息:php 7.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這只是捕獲錯誤,讓我們看一下SomeFileHereCest.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”)而且我看不出問題出在哪里以及如何更深入地研究它。有任何想法嗎?
查看完整描述

3 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

聽起來你的變量沒有設置。


檢查以下 isset 調用:


isset($this->config); 

isset($this->config['backend']);

isset($this->config['backend']['api_prefix']);

您實際上可以在一次 isset 調用 ( isset($x, $y, $z)) 中檢查多個 var,但這會讓您查看具體缺少哪個 var


查看完整回答
反對 回復 2022-06-17
?
森林海

TA貢獻2011條經驗 獲得超2個贊

使用 (??) ( double question mark operator) (" null coalescing operator") 來避免未設置的數組。


這個單元測試給了我“成功”


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);

    }

}


查看完整回答
反對 回復 2022-06-17
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

它與 PHP 7.4 問題有關。解決方案是我們可以將 isset 放在 PHP 或 Laravel Blade 舊代碼中


@foreach ($widgets->get('dashboard') as $widget)

 {!! $widget->render() !!}

@endforeach

使用 isset 更新新代碼


@if(isset($Widget))

@foreach ($widgets->get('dashboard') as $widget)

    {!! $widget->render() !!}

@endforeach

@endif


查看完整回答
反對 回復 2022-06-17
  • 3 回答
  • 0 關注
  • 305 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號