【问题标题】:Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP不推荐使用:与其类同名的方法在 PHP 的未来版本中将不再是构造函数
【发布时间】:2018-11-20 06:48:03
【问题描述】:

我该如何解决这个错误?

error

facebook.php.php 文件:

更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节更多细节

<?php
/**
 * Plugin Name: Facebook Page Widget
 */

// Load widget
add_action( 'widgets_init', 'qwis_facebook_load_widget' );

// Register widget
function qwis_facebook_load_widget() {
    register_widget( 'qwis_facebook_widget' );
}

// Widget class
class qwis_facebook_widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function qwis_facebook_widget() {

        // Widget settings
        $widget_ops = array( 'classname' => 'qwis_facebook_widget', 'description' => __('Facebook page widget', 'qwis_facebook_widget') );

        // Widget control settings
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'qwis_facebook_widget' );

        // Create the widget
        parent::__construct( 'qwis_facebook_widget', __('Facebook Page', 'qwis_facebook_widget'), $widget_ops, $control_ops );

    }

    /**
     * Display widget
     */
    function widget( $args, $instance ) {
        extract( $args );

        // Variables from the widget settings
        $title = apply_filters('widget_title', $instance['title'] );
        $page_url = $instance['page_url'];
        $tabs = $instance['tabs'];
        $header = $instance['header'];
        $cover = $instance['cover'];
        $faces = $instance['faces'];

        // Before widget (defined by theme functions file)
        echo $before_widget;

        // Display widget title
        if ( $title )
            echo $before_title . $title . $after_title;

        ?>
            <div id="fb-root"></div>

            <script>(function(d, s, id) {
              var js, fjs = d.getElementsByTagName(s)[0];
              if (d.getElementById(id)) return;
              js = d.createElement(s); js.id = id;
              js.src = "//connect.facebook.net/ro_RO/sdk.js#xfbml=1&version=v2.7";
              fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));</script>

            <div class="fb-page" data-href="<?php echo esc_url($page_url); ?>" data-tabs="<?php echo ($tabs); ?>" data-small-header="<?php if($header) { echo 'true'; } else { echo 'false'; } ?>" data-adapt-container-width="true" data-hide-cover="<?php if($cover) { echo 'true'; } else { echo 'false'; } ?>" data-show-facepile="<?php if($faces) { echo 'true'; } else { echo 'false'; } ?>"></div>

        <?php

        // After widget (defined by theme functions file)
        echo $after_widget;
    }

    /**
     * Update the widget
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        // Strip tags to remove HTML (important for text inputs)
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['page_url'] = strip_tags( $new_instance['page_url'] );
        $instance['tabs'] = strip_tags( $new_instance['tabs'] );
        $instance['header'] = strip_tags( $new_instance['header'] );
        $instance['cover'] = strip_tags( $new_instance['cover'] );
        $instance['faces'] = strip_tags( $new_instance['faces'] );

        return $instance;
    }


    function form( $instance ) {

        // Set up some default widget settings
        $defaults = array( 'title' => 'Find me on Facebook', 'page_url' => '', 'tabs' => '', 'header' => false, 'cover' => false, 'faces' => false );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
        </p>

        <!-- page_url -->
        <p>
            <label for="<?php echo $this->get_field_id( 'page_url' ); ?>">Facebook Page URL:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'page_url' ); ?>" name="<?php echo $this->get_field_name( 'page_url' ); ?>" value="<?php echo $instance['page_url']; ?>" />
            <small>e.g. http://www.facebook.com/facebook</small>
        </p>

        <!-- tabs -->
        <p>
            <label for="<?php echo $this->get_field_id( 'tabs' ); ?>">Tabs:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'tabs' ); ?>" name="<?php echo $this->get_field_name( 'tabs' ); ?>" value="<?php echo $instance['tabs']; ?>" />
            <small>e.g. timeline, messages, events</small>
        </p>

        <!-- header -->
        <p>
            <input type="checkbox" id="<?php echo $this->get_field_id( 'header' ); ?>" name="<?php echo $this->get_field_name( 'header' ); ?>" <?php checked( (bool) $instance['header'], true ); ?> />
            <label for="<?php echo $this->get_field_id( 'header' ); ?>">Use Small Header</label>
        </p>

        <!-- cover -->
        <p>
            <input type="checkbox" id="<?php echo $this->get_field_id( 'cover' ); ?>" name="<?php echo $this->get_field_name( 'cover' ); ?>" <?php checked( (bool) $instance['cover'], true ); ?> />
            <label for="<?php echo $this->get_field_id( 'cover' ); ?>">Hide Cover Photo</label>
        </p>

        <!-- faces -->
        <p>
            <input type="checkbox" id="<?php echo $this->get_field_id( 'faces' ); ?>" name="<?php echo $this->get_field_name( 'faces' ); ?>" <?php checked( (bool) $instance['faces'], true ); ?> />
            <label for="<?php echo $this->get_field_id( 'faces' ); ?>">Show Friend's Faces</label>
        </p>

    <?php
    }
}

?>

【问题讨论】:

  • 请写 facebook.php 的人更新它

标签: php wordpress facebook widget


【解决方案1】:

一些研究...

你的错误是,

不推荐使用:与其类同名的方法在 PHP 的未来版本中将不再是构造函数

在 google 中输入“methods same name as class php”,前两个结果是 Stack Overflow 问题,为您提供大量信息。

第三个结果是http://php.net/manual/en/language.oop5.decon.php。总是值得一读 php.net 对 PHP 的评价。点击 Google 的链接并向下滚动一点以找到...

为了向后兼容 PHP 3 和 4,如果 PHP 找不到给定类的 __construct() 函数,并且该类没有从父类继承一个,它将搜索旧式构造函数,通过类的名称。

还有……

警告 旧式构造函数在 PHP 7.0 中已弃用,并将在未来版本中删除。您应该始终在新代码中使用 __construct()。


结论...

根据该研究,我们可以确定您需要对代码进行快速更新。换行,

function qwis_facebook_widget() {

function __construct() {

【讨论】:

  • 史蒂夫,首先谢谢!你为我解决了这个问题。您的答案写得好像每个人都应该能够自己解决这个问题,但我认为您低估了这些东西对于凡人来说是多么令人困惑!无论如何,谢谢,您解释了修复并展示了您的工作原理! ;)
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
相关资源
最近更新 更多