【发布时间】:2022-01-15 05:50:43
【问题描述】:
我在 WordPress 插件项目中使用下面的代码。 VS Code 给我一个错误,上面写着:
Method 'Acorn\UI\Mobile\MenuWalker::start_lvl()' is not compatible with method 'Walker_Nav_Menu::start_lvl()'.intelephense(1038)
这是产生错误的代码 sn-p(仅限函数的第一部分):
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。 Eriksson 的请求,这是整个课程:
<?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;
}
}
【问题讨论】:
-
请始终发布所有代码(如原始方法)。如果我们只能看到您的版本而不是原始版本,很难知道问题出在哪里。我们需要知道您使用的确切代码(我们不知道该插件在不同版本中是否有不同的签名以及您使用的是什么版本)。不要指望我们猜测或假设。
标签: php wordpress signature wp-nav-walker