不推荐使用WordPress构造函数

时间:2016-08-14 14:18:54

标签: php wordpress constructor

我想恢复一个旧的WordPress主题。这个主题有一些不推荐使用的功能,所以我试图解决它们。

  

注意:WP_Widget中调用的构造函数方法   从版本4.3.0开始,不推荐使用Custom_Recent_Posts!使用   __construct()代替。

这告诉我WP_Widget的构造函数方法已被弃用,因此必须是这一行:

$this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);

但这条线似乎没问题?我无法找到它的错误。

完整的脚本是这样的:

class Custom_Recent_Posts extends WP_Widget {
    function Custom_Recent_Posts() {
        $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
        $this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);

        echo $before_widget;
        $items = empty($instance['items']) ? ' ' : apply_filters('widget_title', $instance['items']);

        if(!is_numeric($items))
        {
            $items = 3;
        }

        if(!empty($items))
        {
            pp_posts('recent', $items, TRUE);
        }

        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['items'] = strip_tags($new_instance['items']);

        return $instance;
    }

    function form($instance) {
        $instance = wp_parse_args( (array) $instance, array( 'items' => '') );
        $items = strip_tags($instance['items']);

?>
            <p><label for="<?php echo $this->get_field_id('items'); ?>">Items (default 3): <input class="widefat" id="<?php echo $this->get_field_id('items'); ?>" name="<?php echo $this->get_field_name('items'); ?>" type="text" value="<?php echo esc_attr($items); ?>" /></label></p>
<?php
    }
}

register_widget('Custom_Recent_Posts');

任何帮助都会很棒。

现在我有了这个:

class Custom_Recent_Posts extends WP_Widget {
    public function __construct() {
        $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
        $this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);

        echo $before_widget;
        $items = empty($instance['items']) ? ' ' : apply_filters('widget_title', $instance['items']);

        if(!is_numeric($items))
        {
            $items = 3;
        }

        if(!empty($items))
        {
            pp_posts('recent', $items, TRUE);
        }

        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['items'] = strip_tags($new_instance['items']);

        return $instance;
    }

    function form($instance) {
        $instance = wp_parse_args( (array) $instance, array( 'items' => '') );
        $items = strip_tags($instance['items']);

?>
            <p><label for="<?php echo $this->get_field_id('items'); ?>">Items (default 3): <input class="widefat" id="<?php echo $this->get_field_id('items'); ?>" name="<?php echo $this->get_field_name('items'); ?>" type="text" value="<?php echo esc_attr($items); ?>" /></label></p>
<?php
    }
}

register_widget('Custom_Recent_Posts');

3 个答案:

答案 0 :(得分:1)

这应该改为

function Custom_Recent_Posts() {
    $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
    $this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
}

public function __construct() {
    $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
    $this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
}

阅读这些

  1. [closed] constructor method for WP_Widget is deprecated since 4.3.0
  2. The called constructor method for WP_Widget is deprecated since version 4.3.0

答案 1 :(得分:0)

在此代码中:

class Custom_Recent_Posts extends WP_Widget {
    function Custom_Recent_Posts() {
        $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
        $this->WP_Widget('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
    }

该类名为Custom_Recent_Posts,其构造函数也是如此。这是一个PHP 4.x风格的构造函数,从PHP希望如此拼命地成为Java的那些日子开始。

当PHP 5.x出现时,PHP意识到它不需要像Java那样行事。这是它自己的语言,也是一种流行的语言。所以它引入了__construct方法,它看起来像PHP的其他 magic 方法。但它保留了PHP 4.x风格的构造函数,因此它不会破坏任何人的遗留代码。

在WordPress的4.3版本中,他们决定采用新的,更像PHP的__construct语法而不是wannabe-Java方法。这就是你看到错误的原因。要修复它,您需要更改两件事:

  1. 您的类的构造函数需要命名为__construct
  2. 当它调用WP_Widget的构造函数时,需要调用它的__construct。
  3. 第一个很容易。只需将Custom_Recent_Posts()函数重命名为__construct即可。完成。需要5秒钟。

    第二个很容易。只需将$this->WP_Widget更改为$this->__construct,对吗? $this->__construct除外是指您的类的__construct方法。您需要调用parent::__construct来告诉PHP需要调用哪个类的__construct方法。

    所以更正的代码是:

    class Custom_Recent_Posts extends WP_Widget {
        function __construct() {
            $widget_ops = array('classname' => 'Custom_Recent_Posts', 'description' => 'The recent posts with thumbnails' );
            parent::__construct('Custom_Recent_Posts', 'Custom Recent Posts', $widget_ops);
        }
    

    请参阅WordPress Codex上的Widgets API sample code

答案 2 :(得分:0)

function _deprecated_constructor未提供有关受影响的类文件路径的完整信息。将代码(wp内容)从PHP-(较低)升级到PHP-(7.x.x)会花费很多时间


通过将这些行添加到核心函数中,您可以在这里节省很少的时间(我知道这是不好的做法。但是升级完成后,您可以删除此代码!)

/www/wp-content/functions.php Line no:3879

  function _deprecated_constructor( $class, $version, $parent_class = '' ) {

    /**
     * Fires when a deprecated constructor is called.
     *
     * @since 4.3.0
     * @since 4.5.0 Added the `$parent_class` parameter.
     *
     * @param string $class        The class containing the deprecated constructor.
     * @param string $version      The version of WordPress that deprecated the function.
     * @param string $parent_class The parent class calling the deprecated constructor.
     */
    do_action( 'deprecated_constructor_run', $class, $version, $parent_class );

    /**
     * Filters whether to trigger an error for deprecated functions.
     *
     * `WP_DEBUG` must be true in addition to the filter evaluating to true.
     *
     * @since 4.3.0
     *
     * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
     */
    if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) {
        if ( function_exists( '__' ) ) {
            if ( ! empty( $parent_class ) ) {
                /* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */
// addional code start here! --------------------------------------------    
                $reflector = new ReflectionClass($parent_class);
                $filepath = 'filepath :' . $reflector->getFileName();
                trigger_error( sprintf( __( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead. %5$s' ),$class, $parent_class, $version, '<pre>__construct()</pre>', $filepath ) );  
    // addional code end here! ---------------------
            } else {
                /* translators: 1: PHP class name, 2: version number, 3: __construct() method */
                trigger_error( sprintf( __( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ),
                    $class, $version, '<pre>__construct()</pre>' ) );
            }
        } else {
            if ( ! empty( $parent_class ) ) {
                trigger_error( sprintf( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.',
                    $class, $parent_class, $version, '<pre>__construct()</pre>' ) );
            } else {
                trigger_error( sprintf( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
                    $class, $version, '<pre>__construct()</pre>' ) );
            }
        }
    }

}

通过这种方式,您可以受filepath的影响Notice