自PHP 7起不支持使用不赞成使用的PHP4样式类构造函数吗?

时间:2019-04-02 17:15:47

标签: php

不知道我在做什么,但是在阅读其他一些类似的问题时,我想知道在更改之前是否正确

我遇到了错误 自PHP 7起不支持使用不推荐使用的PHP4样式类构造函数 当我在WP Blog上进行兼容性测试时

它引用了这两行

    function boc_latest() {         
        $widget_ops = array('description' => 'Aqua Latest Posts');          
        $this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);           
    }           



function contact_info_widget()      
{       
    $widget_ops = array('classname' => 'contact_info', 'description' => '');    
    $this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops); 
}   

我是否可以像这样简单地更改功能?

    function __construct() {            
        $widget_ops = array('description' => 'Aqua Latest Posts');          
        $this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);           
    }           



function __construct()      
{       
    $widget_ops = array('classname' => 'contact_info', 'description' => '');    
    $this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops); 
}       

这里是完整的php文件

function boc_load_widgets() {

    register_widget('boc_latest');
    register_widget('contact_info_widget');

}   



class boc_latest extends WP_Widget {

        function boc_latest() {
            $widget_ops = array('description' => 'Aqua Latest Posts');
            $this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
        }

        function widget($args, $instance) {
            extract($args, EXTR_SKIP);
            echo $before_widget;
            $title = empty($instance['title']) ? '&nbsp;' : '<span>'.apply_filters('widget_title', $instance['title']).'</span>';
            $count = $instance['count'];

            echo removeSpanFromTitle($before_title) . $title . removeSpanFromTitle($after_title);
            wp_reset_query();
            rewind_posts();

            $recent_posts = new WP_Query(
                array(
                    'posts_per_page' => $count,
                    'post_status' => 'publish',
                    'nopaging' => 0,
                    'post__not_in' => get_option('sticky_posts')
                    )
                );

            // Cycle through Posts    
            if ($recent_posts->have_posts()) :while ($recent_posts->have_posts()) : $recent_posts->the_post();
            ?>

            <div class="boc_latest_post clearfix">
                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('small-thumb'); ?></a>
                <p class="boc_latest_post_title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>
                <p class="date"><?php echo get_the_date();?></p>
            </div>
                <?php
                endwhile;
                endif;
                wp_reset_query();
                rewind_posts();

                echo $after_widget;
            }

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

                $instance['count'] = $new_instance['count'];

                return $instance;
            }

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

                $count = $instance['count'];
                ?>


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

                <p>
                    <label for="<?php echo $this->get_field_id('count'); ?>">How many posts? (Number):
                        <input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
                    </label>
                </p>

                <?php
            }

}


/**
 * Contact Info Widget
 */
class contact_info_widget extends WP_Widget {

    function contact_info_widget()
    {
        $widget_ops = array('classname' => 'contact_info', 'description' => '');
        $this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
    }

    function widget($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        echo $before_widget;

        if($title) {
            echo $before_title.$title.$after_title;
        }
        ?>
        <?php if($instance['phone']): ?>
        <div class="icon_phone"><?php echo $instance['phone']; ?></div>
        <?php endif; ?>

        <?php if($instance['email']): ?>
        <div class="icon_mail"><?php echo $instance['email']; ?></div>
        <?php endif; ?>

        <?php if($instance['address']): ?>
        <div class="icon_loc"><?php echo $instance['address']; ?></div>
        <?php endif; ?>

        <div class="clear h10"></div>

        <?php
        echo $after_widget;
    }

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        $instance['title'] = $new_instance['title'];
        $instance['address'] = $new_instance['address'];
        $instance['phone'] = $new_instance['phone'];
        $instance['fax'] = $new_instance['fax'];
        $instance['email'] = $new_instance['email'];
        $instance['web'] = $new_instance['web'];

        return $instance;
    }

    function form($instance)
    {
        $defaults = array('title' => 'Contact Info');
        $instance = wp_parse_args((array) $instance, $defaults); ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
            <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('phone'); ?>">Phone:</label>
            <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('phone'); ?>" name="<?php echo $this->get_field_name('phone'); ?>" value="<?php echo $instance['phone']; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('email'); ?>">Email:</label>
            <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" value="<?php echo $instance['email']; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('address'); ?>">Address:</label>
            <input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('address'); ?>" name="<?php echo $this->get_field_name('address'); ?>" value="<?php echo $instance['address']; ?>" />
        </p>
    <?php
    }
} 

1 个答案:

答案 0 :(得分:2)

class boc_latest extends WP_Widget {
    function boc_latest(){
       ...
    }
} 

function boc_latest()应该重命名function __construct(),因为类名与方法(函数)名相同。

class boc_latest extends WP_Widget {
    function __construct(){
       ...
    }
} 

普遍适用;如果类包含名称相同的函数(并且不存在__construct),则应将函数/方法重命名为该类的__construct()方法。

来自the Manual

  

为了与PHP 3和4向后兼容,如果PHP找不到给定类的__construct()函数,它将按类名搜索旧式构造函数。

     

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