php - 不推荐使用带有花括号的数组和字符串偏移访问语法
问题描述
我刚刚将我的 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;
}
推荐阅读
- python - 您如何仅“清除”特定的 Flask 会话变量?
- laravel - 跨多列的 HTML 刀片表格单元格中的 Laravel foreach 循环
- python - 在 python 中将 File 作为命令行参数的输入
- reactjs - 无法从节点模块自动导入反应组件
- react-native - 如何计算缩放图像的偏移量?
- google-cloud-platform - Datalab 会自动配置现有的计算引擎吗?
- java - 是否可以无延迟地按需读取 Kinesis 流
- xamarin.forms - 如何动态更改 NavigationPage 栏背景颜色
- jsp - jasperreport 和 JSP 使用 mysql 查询问题
- react-native - React Native:违反使用 Android 广告 ID 政策和开发者分发协议第 4.8 节