提问人:Andy 提问时间:12/20/2022 最后编辑:mickmackusaAndy 更新时间:12/20/2022 访问量:4814
警告:升级到 PHP 8.0 时,尝试访问 bool 类型值的数组偏移量错误 [重复]
Warning: Trying to access array offset on value of type bool error when upgrading to PHP 8.0 [duplicate]
问:
将我们的网站升级到 PHP 8.0 时,我们面临以下错误:
警告:尝试访问 bool 类型值的数组偏移量
这与以下代码行有关:
$meta_value = $meta_values[ $meta_tuple['object_id'] ];
整个函数的代码是:
function wpsc_get_meta( $meta_key, $object_type, $object_id = 0 ) {
global $wpdb;
$cache_object_id = $object_id = (int) $object_id;
$meta_key = wpsc_sanitize_meta_key( $meta_key );
$meta_tuple = compact( 'object_type', 'object_id', 'meta_key' );
$meta_tuple = apply_filters( 'wpsc_get_meta', $meta_tuple );
// Get cached meta
$meta_value = wp_cache_get( $cache_object_id, $meta_tuple['object_type'] );
// If not cached, get and cache all object meta
if ( $meta_value === false ) {
$meta_values = wpsc_update_meta_cache( $meta_tuple['object_type'], $meta_tuple['object_id'] );
$meta_value = $meta_values[ $meta_tuple['object_id'] ];
}
if ( isset( $meta_value[ $meta_tuple['meta_key'] ] ) ) {
return maybe_unserialize( $meta_value[ $meta_tuple['meta_key'] ] );
}
return '';
}
有什么建议我们如何修复上述代码以实现 PHP 8.0 兼容性?
回滚到 PHP 7.4,警告消失。
答:
0赞
Foobar
12/20/2022
#1
您可以更改此设置
if ( isset( $meta_value[ $meta_tuple['meta_key'] ] ) ) {
return maybe_unserialize($meta_value[$meta_tuple['meta_key']]);
}
to(参见 Null 合并运算符)
if ($meta_value[ $meta_tuple['meta_key']] ?? null)
return maybe_unserialize($meta_value[$meta_tuple['meta_key']]);
}
或(老派)
if (
is_array($meta_value)
&& isset( $meta_value[ $meta_tuple['meta_key'] ] )
) {
return maybe_unserialize($meta_value[$meta_tuple['meta_key']]);
}
评论
0赞
Andy
12/21/2022
谢谢大家的建议!
评论
if ( is_array($meta_value) && isset( $meta_value[ $meta_tuple['meta_key'] ] ) ) {
$meta_value