【问题标题】:is this correct to fix Use of deprecated PHP4 style class constructor is not supported since PHP 7?自 PHP 7 以来不支持使用已弃用的 PHP4 样式类构造函数,这是否正确修复?
【发布时间】:2019-04-02 17:15:46
【问题描述】:

不知道我在做什么,但是在阅读其他一些类似的问题时,我想知道这是否正确,然后再进行更改

我收到了错误 自 PHP 7 起不支持使用已弃用的 PHP4 样式类构造函数 当我在我的 WP 博客上运行兼容性测试时

它引用了这两行

    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
    }
} 

【问题讨论】:

  • 只是检查一下,这两个单独的类是否名为“boc_latest”和“contact_info_widget”?
  • 从你所展示的,是的。
  • 不确定是否有 2 个单独的类,所以我添加了完整的 php 文件
  • 好吧,@MShack,感谢您提供完整的文件(因为它有助于给出明确的答案)。它两个独立的类,所以是的,您可以将它们替换为__construct
  • 您在使用 10 年前的代码时可能会遇到其他问题,我会非常谨慎地这样做。

标签: php


【解决方案1】:
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()

【讨论】:

  • 谢谢!同名部分让我失望。我没有意识到它有一个与类同名的私有函数。
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 2020-01-16
  • 2014-02-04
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多