PHP walker 类数组和对象


PHP walker class arrays and objects

我正在与walker类合作为WordPress主题创建一个菜单,我拥有的代码是;

class Walker_Nav_Primary extends Walker_Nav_menu {
    function start_lvl( &$output, $depth = 0, $args = array() ){ //ul
        $indent = str_repeat("'t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "'n$indent<ul class='"dropdown-menu$submenu depth_$depth'">'n";
    }
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
        $indent = ( $depth ) ? str_repeat("'t",$depth) : '';
        $li_attributes = '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = ($args->walker->has_children) ? 'dropdown' : '';
        $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
        $classes[] = 'menu-item-' . $item->ID;
        if( $depth && $args->walker->has_children ){
            $classes[] = 'dropdown-submenu';
        }
        $class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="' . esc_attr($class_names) . '"';
        $id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr($item->url) . '"' : '';
        $attributes .= ( $args->walker->has_children ) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
        $item_output = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= ( $depth == 0 && $args->walker->has_children ) ? ' <b class="caret"></b></a>' : '</a>';
        $item_output .= $args->after;
        $output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

但是,当一个朋友查看主题时,他看到了下面的消息,我和其他人都没有看到这条消息,菜单工作正常。

严格标准:声明Walker_Nav_Primary::start_lvl() 应兼容 Walker_Nav_Menu::start_lvl(&$output,$depth = 0, $args = 数组) 在 .../inc/walker.php 在第 0 行

所以他做了一些更改,首先是通过在缺少的start_lvl函数中放入 $args =array() 来解决此错误,但随后他写道$args不是一个对象,而是一个数组,所以你不能做$args->某事,你必须做$args['某事'],这给出了代码;

class Walker_Nav_Primary extends Walker_Nav_menu {
    function start_lvl( &$output, $depth = 0, $args = array() ) { //ul
        $indent = str_repeat("'t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "'n$indent<ul class='"dropdown-menu$submenu depth_$depth'">'n";
    }
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ){ //li a span
        $indent = ( $depth ) ? str_repeat("'t",$depth) : '';
        $li_attributes = '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = ($args['walker']->has_children) ? 'dropdown' : '';
        $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
        $classes[] = 'menu-item-' . $item->ID;
        if( $depth && $args['walker']->has_children ){
            $classes[] = 'dropdown-submenu';
        }
        $class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="' . esc_attr($class_names) . '"';
        $id = apply_filters('nav_menu_item_id', 'menu-item-'.$item->ID, $item, $args);
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr($item->url) . '"' : '';
        $attributes .= ( $args['walker']->has_children ) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
        $item_output = $args['before'];
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args['link_before'] . apply_filters( 'the_title', $item->title, $item->ID ) . $args['link_after'];
        $item_output .= ( $depth == 0 && $args['walker']->has_children ) ? ' <b class="caret"></b></a>' : '</a>';
        $item_output .= $args['after'];
        $output .= apply_filters ( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

但是,当我运行代码时,出现以下错误

致命错误:不能将 stdClass 类型的对象用作数组

指以下行:

$classes[] = ($args->walker->has_children) ? 'dropdown' : '';

已更改为

$classes[] = ($args['walker']->has_children) ? 'dropdown' : '';

我们都在同一个PHP和WordPress上,所以我不明白为什么它对他而不是我有用,哪种是正确的方法。

谁能透露任何光芒?

如果您确定$args是一个数组,请将此代码放在使用$args

if (!isset($args['walker']) || !(is_object($args['walker']) && isset($args['walker']->has_children)) {
   $classes[] = '';
   $args = (object) array('walker' => array('has_children' => false));
}

但是,如果$args对象,则使用以下:

if (!isset($args->walker) || !(is_object($args->walker) && isset($args->walker->has_children)) {
   $classes[] = '';
   $args = (object) array('walker' => array('has_children' => false));
}

如果不一致,首先要使其保持一致,请确保$args数组或对象。