PHP已弃用:不应静态调用非静态方法

时间:2017-11-19 作者:Cynthia A Lockley

我的调试。日志中有以下消息指向/wp includes/shortcode。php第319行,这是WordPress核心的一部分,可能不是代码中真正的错误所在。

PHP Deprecated:  Non-static method RSSjbClass::RSSjb_funct() should not be called statically in /.../wp-includes/shortcodes.php on line 319
我看了319行,但它并没有帮助我找到或解决问题。我搜索了代码,找到了这个类和funct,但我不知道静态调用什么是非静态的。

/**
 * Add shortcode to wordpress
 */
// add_shortcode(\'RSSjb\', \'RSSjb_funct\');
add_shortcode(\'RSSjb\', array(\'RSSjbClass\', \'RSSjb_funct\'));
/**
 * Shortcode Class and Function
 */
class RSSjbClass {
    function RSSjb_funct($atts) {
        extract(shortcode_atts(array(
        // attributes for Google News
        "gsearch" => \'\',
        "topic" => \'\',
        "location" => \'\',
        "geo" => \'\',
        "feed" => \'\', // required
        "filter" => \'\',
        "num" => \'5\',
        "ltime" => \'\',
        "list" => \'ul\',
        "target" => \'_blank\',
        "pubdate" => \'false\',
        "pubtime" => \'false\',
        "dformat" => get_option(\'date_format\'),
        "tformat" => get_option(\'time_format\'),
        "pubauthor" => \'true\',
        "excerpt" => \'false\',
        "charex" => \'\',
        "chartle" => \'\',
        "title" => \'\',
        "link" => \'false\',
        "sort" => \'false\',
        "cachefeed" => \'3600\'
    ), $atts));
然后是几个“如果”,然后

    // call the function to read and display the feed items list
        return $tle . rssjb_List($feed, $filter, $num, $ltime, $list, $target, $pubdate, $pubtime, $dformat, $tformat, $pubauthor, $excerpt, $charex, $chartle, $sort, $cachefeed);
    } else {
        return \'<br>RSS or Atom Feed URL not provided. This shortcode does require the attribute feed or location (if Google News).<br /> Ex: <code>[RSSjb feed = "http://your-rss-or-atom-feed-URL-here"]</code>.\'; 
    }
    }
}
如何修改它以适应/wp includes/shortcode中的内容。php第319行,即:

    $output = $m[1] . call_user_func( $shortcode_tags[ $tag ], $attr, $content, $tag ) . $m[6];

    /**
     * Filters the output created by a shortcode callback.
     *
     * @since 4.7.0
     *
     * @param string       $output Shortcode output.
     * @param string       $tag    Shortcode name.
     * @param array|string $attr   Shortcode attributes array or empty string.
     * @param array        $m      Regular expression match array.
     */
    return apply_filters( \'do_shortcode_tag\', $output, $tag, $attr, $m );
}
我搜索了PHP手册,它没有任何关于短代码的内容。

2 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

短代码是WordPress的东西,因此不在PHP手册中。您想在PHP手册中看到的是section on callbacks. add_shortcode() 接受短代码名称和回调。在PHP手册的这一部分中,您可以看到3种主要类型的回调:

// Type 1: Simple callback
call_user_func(\'my_callback_function\');

// Type 2: Static class method call
call_user_func(array(\'MyClass\', \'myCallbackMethod\'));

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, \'myCallbackMethod\'));
短代码的回调函数是第二种类型:

add_shortcode(\'RSSjb\', array(\'RSSjbClass\', \'RSSjb_funct\'));
这和打电话一样RSSjbClass::RSSjb_funct, 这就是调用静态方法的方式,但在类中RSSjb_funct() 方法不是静态定义的:

function RSSjb_funct($atts) {
因此,最快的修复方法是将该方法定义为static:

public static function RSSjb_funct($atts) {
另一个选项是使用$this, 或者创建类的实例,然后在add_shortcode() 与上述类型3类似。

SO网友:Shane O

如果不想静态调用类方法,可以执行以下操作

add_shortcode(\'RSSjb\', [new RSSjbClass, \'RSSjb_funct\']);

结束