admin管理员组

文章数量:1310392

my server just upgrades at Dreampress and it appears to be throwing errors to the log. : Got error 'PHP message: PHP Warning: Declaration of My_Walker_Nav_Menu::start_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = NULL) in /home/wp_bizvdr/ctisinc/wp-content/themes/ctis/functions.php on line 5',

Since it happens on every page load, its adding up to the log and potentially broke our site for some time. At least that is what Dreamhost is telling us.

This is the code its referring to I think. And I seems to be written correctly but wanted to get your take.

class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"dropdown-menu\">\n"; } } Any ideas?

my server just upgrades at Dreampress and it appears to be throwing errors to the log. : Got error 'PHP message: PHP Warning: Declaration of My_Walker_Nav_Menu::start_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = NULL) in /home/wp_bizvdr/ctisinc/wp-content/themes/ctis/functions.php on line 5',

Since it happens on every page load, its adding up to the log and potentially broke our site for some time. At least that is what Dreamhost is telling us.

This is the code its referring to I think. And I seems to be written correctly but wanted to get your take.

class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"dropdown-menu\">\n"; } } Any ideas?

Share Improve this question asked Feb 4, 2021 at 1:26 TomTom 2351 gold badge2 silver badges8 bronze badges 3
  • In your function declaration, the $depth needs to defined as $depth = 0. I.e. The original function made it optional by setting a default value, and so should your function. – Sally CJ Commented Feb 4, 2021 at 2:39
  • Thanks Sally CJ - Something like this class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = Array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"dropdown-menu dropit\">\n"; } } – Tom Commented Feb 4, 2021 at 4:44
  • Yes, and if my (revised) answer answered the question, I'd appreciate it if you can mark my answer as correct. :) – Sally CJ Commented Feb 5, 2021 at 4:12
Add a comment  | 

1 Answer 1

Reset to default 1

In short, (and as I said in the comments) in your function declaration, the $depth parameter should be defined as $depth = 0. And that's because the original function (or class method) made it optional by setting a default value, so you should also do the same:

function start_lvl( &$output, $depth = 0, $args = array() )

And if you wonder why so or why the warning was thrown by PHP, then check the PHP manual about "Signature compatibility rules" which says:

When overriding a method, its signature must be compatible with the parent method. Otherwise, a fatal error is emitted, or, prior to PHP 8.0.0, an E_WARNING level error is generated. A signature is compatible if it respects the variance rules, makes a mandatory parameter optional, and if any new parameters are optional. This is known as the Liskov Substitution Principle, or LSP for short. The constructor, and private methods are exempt from these signature compatibility rules, and thus won't emit a fatal error in case of a signature mismatch.

And on that page, there are actually examples demonstrating that a child method which removes a parameter, or makes an optional parameter mandatory, is not compatible with the parent method.

So I hope that helps. :)

本文标签: functionsUpgraded server now class MyWalkerNavMenu extends WalkerNavMenu not working