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?
1 Answer
Reset to default 1In 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, andprivate
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
版权声明:本文标题:functions - Upgraded server now class My_Walker_Nav_Menu extends Walker_Nav_Menu not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741773811a2396908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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