You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP8中$wpsc_query->query属性读取空值警告的修复方案

解决PHP8中「Attempt to read property "query" on null」警告的最优方案

问题根源

PHP8对访问null对象属性的行为做了严格检查,当全局变量$wpsc_query未被初始化(值为null)时,直接调用$wpsc_query->query会触发明确警告——这在低版本PHP中可能只会静默忽略,但PHP8会抛出「Attempt to read property "query" on null」的提示。

最优修改方案

核心是先校验$wpsc_query的存在性与非空状态,再判断其query属性是否为数组。修改触发警告的代码行:

原代码行:

if ( ! is_array( $wpsc_query->query ) ) {

修改为:

if ( ! isset( $wpsc_query ) || ! is_array( $wpsc_query->query ) ) {

完整修改后的代码

function wpsc_body_class( $classes ) {
    global $wp_query, $wpsc_query;
    $post_id = get_the_ID();

    if ( $post_id ) {
        $page_url = get_permalink( $post_id );

        // If on a product or category page...
        if ( get_option( 'product_list_url' ) == $page_url || get_post_type( $post_id ) === 'wpsc-product' ) {
            $classes[] = 'wp-e-commerce';
            
            // 修复PHP8警告:先检查$wpsc_query是否存在且非空,再判断query属性
            if ( ! isset( $wpsc_query ) || ! is_array( $wpsc_query->query ) ) {
                $classes[] = 'wpsc-home';
            }

            if ( wpsc_is_single_product() ) {
                $object    = $wp_query->get_queried_object();
                $classes[] = 'wpsc-single-product';
                if ( absint( $object->ID ) > 0 ) {
                    $classes[] = 'wpsc-single-product-' . absint( $object->ID );
                }
            }

            if ( wpsc_is_in_category() && ! wpsc_is_single_product() ) {
                $classes[]  = 'wpsc-category';
                $tax_object = $wp_query->get_queried_object();
                $classes[]  = 'wpsc-category-' . esc_attr( $tax_object->slug );
            }

            if ( wpsc_is_in_tag() && ! wpsc_is_single_product() ) {
                $classes[]  = 'wpsc-tag';
                $tax_object = $wp_query->get_queried_object();
                $classes[]  = 'wpsc-tag-' . esc_attr( $tax_object->slug );
            }
        }

        $page_url = set_url_scheme( $page_url, 'relative' );

        // If viewing the shopping cart...
        if ( set_url_scheme( get_option( 'shopping_cart_url' ), 'relative' ) === $page_url ) {
            $classes[] = 'wp-e-commerce';
            $classes[] = 'wpsc-shopping-cart';
        }

        // If viewing the transaction...
        if ( set_url_scheme( get_option( 'transact_url' ), 'relative' ) === $page_url ) {
            $classes[] = 'wp-e-commerce';
            $classes[] = 'wpsc-transaction-details';
        }

        // If viewing your account...
        if ( set_url_scheme( get_option( 'user_account_url' ), 'relative' ) === $page_url ) {
            $classes[] = 'wp-e-commerce';
            $classes[] = 'wpsc-user-account';
        }
    }

    return $classes;
}

修改说明

  • isset( $wpsc_query )确保全局变量已定义且不为null,避免直接访问null对象属性触发警告;
  • 逻辑完全匹配原代码:当$wpsc_query未定义/为null,或其query属性不是数组时,添加wpsc-home类;
  • 兼容PHP8及所有低版本PHP,不会破坏原有业务逻辑。

内容的提问来源于stack exchange,提问作者Andy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 06:45:19