Magento 2 Upgrade Error: Deprecated Functionality: json_decode()

更新

升级到Magento 2.4.6-p3后,文件报错:


Deprecated Functionality: json_decode(): Passing null to parameter \#1 ($json) of type string is deprecated in...

这个错误提示是因为在使用 json_decode() 函数时,传递了 null 或者非字符串类型的参数,而该参数应该是一个字符串类型。这个问题在 PHP 7.4 版本中被标记为废弃,意味着在未来的版本中可能会被移除。

我是从 Magento2.4.3升级到 2.4.6,PHP 版本从 7.4升级到了 8.1,所以遇到这个问题。

要解决这个问题,需要检查代码中使用 json_decode() 函数的地方,确保第一个参数传递的是一个字符串类型的值。如果第一个参数可能为空,可以使用条件语句进行判断,例如:


if ($json) {
    $data = json_decode($json, true);
} else {
    $data = [];
}

这样就可以避免将 null 或者非字符串类型的参数传递给 json_decode() 函数,从而避免出现这个错误。