Domanda

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.com/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?

È stato utile?

Soluzione

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. :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top