php 不推荐使用带花括号的数组和字符串偏移访问语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/59158548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array and string offset access syntax with curly braces is deprecated
提问by Pezhvak
I've just updated my php version to 7.4, and i noticed this error pops up:
我刚刚将我的 php 版本更新到 7.4,我注意到这个错误弹出:
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?
我的项目中很少有库使用花括号来获取字符串中的单个字符,解决此问题的最佳方法是什么?
回答by Pezhvak
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:
由于 php 7.4 花括号获取字符串中单个字符的方法已被弃用,因此将上述语法更改为:
$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.
希望这也能帮助其他人。