【问题标题】:Code runs fine in php 7 but returns error in php 8代码在 php 7 中运行良好,但在 php 8 中返回错误
【发布时间】:2022-01-26 07:34:48
【问题描述】:

我有下面的代码在 php 7 中运行良好。但是当我将 php 更改为版本 8 时,它给了我一个错误

代码

class WC_plugin {
        
    public static function init() {
        global $wp_filter;
        global $post;
        global $wpdb;
        add_action( 'woocommerce_update_product', __CLASS__ . '::createXml' );
    }
    
    
    function createXml($product_id){
        // Do something
        

    }
    
}

WC_plugin::init();

我得到的错误是下面的错误

Argument #1 ($callback) must be a valid callback, non-static method WC_plugin::createXml() cannot be called statically

任何人都可以帮助解决上述问题吗?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    问题似乎很明确:

    Argument #1 ($callback) must be a valid callback, non-static method WC_plugin::createXml() cannot be called statically
    

    如果该方法不是静态的,则不能静态调用方法(使用::)...

    createXml() 方法不是静态方法,因此您不能在 add_action 中这样调用它:WC_plugin::createXml();

    也许这应该会更好

    add_action( 'woocommerce_update_product', [ $this ,'createXml' ] );
    

    对应如下调用WC_plugin->createXml();

    我希望这个答案对你有用。

    【讨论】:

    • 我试试看,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多