我刚刚将我的 php 版本更新到 7.4,我注意到弹出了这个错误:
不推荐使用带大括号的数组和字符串偏移访问语法
这是我的代码的一部分,它触发了上述错误:
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;
}
我的项目中很少有库使用大括号来获取字符串中的单个字符,改变语法问题的最佳方法是什么?
解决这个问题非常简单,但是请记住,您应该为您在其存储库中使用的每个库分叉并提交更改,以帮助其他人。
假设您的代码中有这样的内容:
$str = "test";
echo($str{0});
由于 PHP 7.4 获取字符串中单个字符的大括号方法已被弃用,因此将上述语法更改为:
$str = "test";
echo($str[0]);
修复问题中的代码将如下所示:
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)