简体   繁体   English

代码在 php 7 中运行良好,但在 php 8 中返回错误

[英]Code runs fine in php 7 but returns error in php 8

i have the code bellow that runs fine in php 7. But when i change php to version 8 it gives me an error我有下面的代码在 php 7 中运行良好。但是当我将 php 更改为版本 8 时,它给了我一个错误

The code编码

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();

And the error i am getting is the bellow one我得到的错误是下面的错误

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

Can anyone help with the above?任何人都可以帮助解决上述问题吗?

The problem seems to be clearly defined:问题似乎很明确:

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

You can't call a method statically (with :: ) if this method is not static...如果此方法不是 static,则不能静态调用方法(使用:: )...

createXml() method is not a static method so you can't call it like this in your add_action: WC_plugin::createXml(); createXml()方法不是 static 方法,因此您不能在 add_action 中这样调用它: WC_plugin::createXml();

Maybe this should work better也许这应该更好

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

Which corresponds to the following call WC_plugin->createXml();对应于下面的调用WC_plugin->createXml();

I hope this answer will be useful to you.我希望这个答案对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM