不建议使用带有花括号的数组和字符串偏移量访问语法 [英] Array and string offset access syntax with curly braces is deprecated

查看:1176
本文介绍了不建议使用带有花括号的数组和字符串偏移量访问语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的php版本更新为7.4,我注意到此错误弹出:

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?

推荐答案

解决该问题确实很简单,但是请记住,您应该对存储库中使用的每个库进行派生并提交所做的更改,以帮助其他人好吧.

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

由于不赞成使用php 7.4大括号方法来获取字符串中的单个字符,因此将上述语法更改为:

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.

这篇关于不建议使用带有花括号的数组和字符串偏移量访问语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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