PHP 7类方法兼容性问题

时间:2017-08-29 作者:Oksana Romaniv

有人能帮我确定升级到php 7.0后,该文件中的哪个函数/语句会导致致命错误吗?在php 5.6上,一切正常。

错误:“theme\\u navigation::update()的声明应与第0行的WP\\u小部件::update($new\\u instance,$old\\u instance)”兼容

代码:

class theme_navigation extends WP_Widget {
public function __construct() {
    parent::__construct(
        \'theme_navigation\', // Base ID
        \'Child Page Navigation\', // Name
        array(\'description\' => \'\') // Args
    );
}

function form($instance){
    $instance[\'tiutle\'] = apply_filters( \'widget_title\', $instance[\'title\'] ); ?>
  <p>Title: <input name="cpn_title" id="cpn_title" type="text" <?php if(isset($instance[\'title\']) && $instance[\'title\'] != "") echo \'value="\'.$instance[\'title\'].\'"\'; ?> /></p>
  <p>Sort by Sort Order? <input name="cpn_sort" id="cpn_sort" type="checkbox" <?php if(isset($instance[\'sort\']) && $instance[\'sort\']) echo \'checked="checked"\'; ?> value="1" /></p>
<?php }

function update(){
    if(!isset($_POST[\'cpn_sort\']))
        $_POST[\'cpn_sort\'] = 0;

    foreach($_POST as $field => $val){
        if(substr($field,0,4) != "cpn_")
            continue;

        $opts[substr($field,4)] = attribute_escape($_POST[$field]);
        unset($_POST[$field]);
    }

    return $opts;
}

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

    $page_arr = array(\'child_of\' => $post->ID, \'parent\' => $post->ID);

    if($instance[\'sort\'])
        $page_arr[\'sort_column\'] = \'menu_order\';

    $pages = get_pages($page_arr);

    if($post->post_parent && !count($pages))
        $page_arr[\'child_of\'] = $page_arr[\'parent\'] = $post->post_parent;
        $pages = get_pages($page_arr);

    if(!count($pages))
        return;

    echo $args[\'before_widget\'];

    if(isset($instance[\'title\']) && $instance[\'title\'] != "")
        echo $args[\'before_title\'].$instance[\'title\'].$args[\'after_title\'];

    echo "<ul>";

    foreach($pages as $page){
        echo "<li><a href=\'".get_permalink($page->ID)."\'>".get_the_title($page->ID)."</a></li>";
    }

    echo "</ul>".$args[\'after_widget\'];
}
}
add_action( \'widgets_init\', create_function( \'\', \'register_widget( "theme_navigation" );\' ) );

1 个回复
最合适的回答,由SO网友:Anton Lukin 整理而成

类继承上的函数中必须具有相同的函数参数。

所以更换function update() 具有update($new_instance, $old_instance) 在您的代码中

结束

相关推荐

Two menus show up

如果我将“echo”参数值设置为“true”:<?php if( has_nav_menu( \'primary-menu\' ) ) { wp_nav_menu( $menuParameters = array( \'theme_location\' => \'primary-menu\', \'container\'