Problem: PHP Warning:  Declaration of ... should be compatible with ....

You will see this error when you (or your hosting company) upgrades to the latest php 7 which is great.

This is wp-cli output before the warnings were suppresses.

PHP Warning: Declaration Of ... Should Be Compatible With .... before

PHP Warning: Declaration Of ... Should Be Compatible With .... before

This is wp-cli output after the warnings were suppressed

 PHP Warning: Declaration Of ... Should Be Compatible With .... after

PHP Warning: Declaration Of ... Should Be Compatible With .... after

 

The other problem is that based on the error reporting settings php will could show those warning messages to your users and/or in your log files. It would be stupid to run out of space because of those warnings. Suppressing all of the warnings is not a good idea as it will make the troubleshooting harder when it's needed.

There are multiple ways to fix this.

Solution #1

One of the ways to remove this php warning is that you will have to go and inspect the code where the warnings are coming from. This could be plugins/themes which contain php classes. It seems something has changed and php 7 doesn’t like it. This is because some method's parameters don’t method(s) those in their the parent class’ or vice versa.  Then fix the parameters so they match.
This solution is long term, however, if you have full control over the code.

Solution #2

Ignore the warning and suppress it. In the context of WordPress plugins/themes it’s a tedious work to go through and fix each plugin or theme’s files. This is also pointless because a new update would override your changes anyways. This is the approach I’d recommend for the time being while the developers fix their code in the upcoming months.

You can use this code in a file that's get included first. This is usually a config file. In WordPress case you can add it to wp-config.php (right after the opening php tag: <?php or to your functions.php file.

If you'd rather install a WordPress plugin please check Orbisius Warning Suppressor plugin.

 

// Call the function to install the warning suppressor
orbisius_p3778_warning_suppressor_supress_warnings();

/**
 * Installs an error handler that will be called only for php E_WARNING
 * @package Orbisius Warning Suppressor
 * @since 1.0
 */
function orbisius_p3778_warning_suppressor_supress_warnings() {
    // How to turn it off via wp-config.php
    if ( version_compare( phpversion(), 7, '>=' ) ) {
        // https://stackoverflow.com/questions/36079651/silence-declaration-should-be-compatible-warnings-in-php-7
        set_error_handler('orbisius_p3778_warning_suppressor_suppress_bad_warnings', E_WARNING);
    }
}

/**
 * Suppresses only the weird messages about the wrong/incorrect declaration.
 * @param int $err_no
 * @param str $err_str
 * @return bool
 */
function orbisius_p3778_warning_suppressor_suppress_bad_warnings($err_no, $err_str, $errfile = '', $errline = '') {
    // If the function returns FALSE then the normal error handler continues.
    $contains_stupid_warning = stripos($err_str, 'Declaration of') !== false;
    return $contains_stupid_warning;
}

 

Were you also have been searching for:

  • How to Suppress PHP Warning: Declaration of ... should be compatible with ....
  • How to Remove PHP Warning: Declaration of ... should be compatible with ....
  • How to Get rid of PHP Warning: Declaration of ... should be compatible with ....