我在 WordPress 插件專案中使用下面的代碼。VS Code 給我一個錯誤,上面寫著:
Method 'Acorn\UI\Mobile\MenuWalker::start_lvl()' is not compatible with method 'Walker_Nav_Menu::start_lvl()'.intelephense(1038)
這是生成錯誤的代碼片段(僅限函式的第一部分):
namespace Acorn\UI\Mobile;
class MenuWalker extends \Walker_Nav_Menu {
private int $depth = 0;
public function start_lvl(
string &$output,
int $depth = 0,
\stdClass $args = []
) {
$this->_set_depth( $depth );
$output .=
$this->_dump( '<start_lvl />' ) .
$this->_dump( $args )
;
}
更新 我遵循@Spoody 的建議并將函式更改為:
namespace Acorn\UI\Mobile;
class MenuWalker extends \Walker_Nav_Menu {
private int $depth = 0;
public function start_lvl( &$output, $depth = 0, \stdClass $args = [] ) {
$this->_set_depth( (int) $depth );
$output .=
$this->_dump( '<start_lvl />' ) .
$this->_dump( $args )
;
}
...但我仍然收到同樣的錯誤。還有其他想法嗎?
更多細節 在@M。埃里克森的要求,這是整個班級:
<?php
namespace Acorn\UI\Mobile;
class MenuWalker extends \Walker_Nav_Menu {
private int $depth = 0;
public function start_lvl( &$output, $depth = 0, \stdClass $args = null ) {
$this->_set_depth( (int) $depth );
$output .=
$this->_dump( '<start_lvl />' ) .
$this->_dump( $args )
;
}
// public function end_lvl(
// string &$output,
// int $depth = 0,
// \stdClass $args = null
// ) : void {
// $this->_set_depth( $depth );
// $output .=
// $this->_dump( '<end_lvl />' ) .
// $this->_dump( $args )
// ;
// return;
// }
// public function start_el(
// string &$output,
// \WP_Post $item,
// int $depth = 0,
// \stdClass $args = $this->emptyStdClass,
// int $id
// ) : void {
// $this->_set_depth( $depth );
// $output .=
// $this->_dump( '<start_el />' ) .
// $this->_dump( "ID:", $id ) .
// $this->_dump( 'Post:', $item ) .
// $this->_dump( 'Object:', $args )
// ;
// return;
// }
// public function end_el(
// string &$output,
// \WP_Post $item,
// int $depth = 0,
// \stdClass $args = $this->emptyStdClass
// ) : void {
// $this->_set_depth( $depth );
// $output .=
// $this->_dump( '<end_el />' ) .
// $this->_dump( 'Post:', $item ) .
// $this->_dump( 'Object:', $args )
// ;
// return;
// }
private function _dump( mixed ...$args ) : string {
$output = '';
foreach ( $args as $obj ) {
$type = gettype( $obj );
$output .=
match ( $type ) {
'boolean' => $this->_tab() . $obj ? 'true' : 'false',
'string',
'integer',
'double' => $this->_tab() . (string) $obj,
'array',
'object' => $this->_indent_text_block( var_export( $obj, true ) ),
default => $this->_tab() . "Type '{$type}' is invalid for an argument to _dump()"
} .
"\n"
;
}
return $output;
}
private function _tab( int $spaces_per_tab = 4 ) : string {
if ( $spaces_per_tab < 0 ) {
throw new \InvalidArgumentException( "Spaces-per-tab parameter < 0: {$spaces_per_tab}" );
}
return str_repeat( ' ', $spaces_per_tab * 4 );
}
private function _indent_text_block( string $str ) : string {
$lines = explode( "\n", $str );
$lines = array_map( fn( $line ) => $this->_tab . "{$line}\n", $lines );
return implode( "\n", $lines );
}
private function _set_depth( int $depth ) : int {
if ( $depth < 0 ) {
throw new \InvalidArgumentException( "Input parameter \$depth < 0: {$depth}" );
}
$this->depth = $depth;
return $this->depth;
}
}
uj5u.com熱心網友回復:
您在start_lvl()的引數中使用型別,但\Walker_Nav_menu不是。
從源代碼
/**
* Starts the list before the elements are added.
*
* @since 3.0.0
*
* @see Walker::start_lvl()
*
* @param string $output Used to append additional content (passed by reference).
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
public function start_lvl( &$output, $depth = 0, $args = null )
您需要從方法的引數中洗掉型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411696.html
標籤:
