从 PHP7.4 切换到 PHP8.1 致命错误:未捕获错误:尝试在第 93 行的 null 中分配属性"posts" 98 224 232



试图将WordPress网站从PHP7.4更新到8.1,我收到了这些错误

致命错误:未捕获错误:尝试分配属性"帖子";在null上u2028在TEMPLATEFILE的第93行

Line 93 
$query_txt_ref->posts = array();
Line 98
$query_book->posts = array();
Line 224
$query_txt_ref_others->posts = array();
Line 232
$query_others->posts = array();

我在7.4中工作,但不知道如何改变它。有人能给我一些关于如何改变它的建议吗?

这里还有几行可供参考。

add_filter('posts_where', 'where_text_refrence');
add_filter('posts_join','join_text_refrence');
$query_txt_ref->posts = array();
$query_txt_ref = new WP_Query($args_daily_devotion_txt_ref);
remove_filter('posts_where', 'where_text_refrence');
remove_filter('posts_join', 'join_text_refrence');
$query_book->posts = array();
add_filter('posts_where', 'where_botb_tags');
$query_book = new WP_Query($args_daily_devotion);
remove_filter('posts_where', 'where_botb_tags');

->foo语法指对象属性。在PHP/8之前,当您尝试在null变量中写入属性时,PHP会自动为您创建一个对象:

$query_txt_ref = null;
$query_txt_ref->posts = array();
var_dump($query_txt_ref);
object(stdClass)#1 (1) {
["posts"]=>
array(0) {
}
}

PHP多年来一直对此发出警告(先发出警告,然后发出警告(。目前,您不再获得对象,并且会引发致命错误。

演示

如果你依赖于对象被自动初始化,你现在需要明确:

$query_txt_ref = new stdClass();
$query_txt_ref->posts = array();

如果它掩盖了一个错误,你需要从源头上修复它。找到$query_txt_ref的初始化位置,并找出为什么它有时不是一个对象。如果这是可以预料的事情,你需要处理它:

if ($query_txt_ref === null) {
// Do something
} else {
$query_txt_ref->posts = array();
}

如果不可能发生,则在初始化之前会在某个地方找到问题。

最新更新