Array and string offset access syntax with curly braces is deprecated [duplicate]

落爺英雄遲暮 提交于 2020-01-23 07:56:52

问题


I've just updated my php version to 7.4, and i noticed this error pops up:

Array and string offset access syntax with curly braces is deprecated

here is part of my code which is triggering the above error:

public function getRecordID(string $zoneID, string $type = '', string $name = ''): string
{
    $records = $this->listRecords($zoneID, $type, $name);
    if (isset($records->result{0}->id)) {
        return $records->result{0}->id;
    }
    return false;
}

there are few libraries in my project which is using curly braces to get individual characters inside a string, whats the best way to fix this issue?


回答1:


it's really simple to fix the issue, however keep in mind that you should fork and commit your changes for each library you are using in their repositories to help others as well.

lets say you have something like this in your code:

$str = "test";
echo($str{0});

since php 7.4 curly braces method to get individual characters inside a string has been deprecated, so change the above syntax into this:

$str = "test";
echo($str[0]);

fixing the code in the question will look something like this:

public function getRecordID(string $zoneID, string $type = '', string $name = ''): string
{
    $records = $this->listRecords($zoneID, $type, $name);
    if (isset($records->result[0]->id)) {
        return $records->result[0]->id;
    }
    return false;
}

hope this help others as well.



来源:https://stackoverflow.com/questions/59158548/array-and-string-offset-access-syntax-with-curly-braces-is-deprecated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!