Last active
December 20, 2018 13:48
-
-
Save ameenross/a6f17555156d579222cb4e0a99623670 to your computer and use it in GitHub Desktop.
PHP 7.2 suppress warning from count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is an alternative way of handling warnings from calling count() on | |
* non-countables. It's just an error handler that drops the warning, and calls | |
* the previous error handler for everything else (if there is one). | |
*/ | |
$previous = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$previous) { | |
// Do nothing with an E_WARNING from count. | |
if ($errstr == 'count(): Parameter must be an array or an object that implements Countable') { | |
return; | |
} | |
// If another error handler was defined, call it. | |
if ($previous) { | |
return $previous($errno, $errstr, $errfile, $errline, $errcontext); | |
} else { | |
// Use the default error handler. | |
return false; | |
} | |
}, E_WARNING); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: https://gist.github.com/ameenross/e7f4dea8468d178464ca2f221ede72c0 for some more context.
Make sure this error handler is the last one to be added. Put your code in a place so that it is run after other set_error_handler calls.
In Symfony 2.x, a suitable place would be
AppKernel::registerContainerConfiguration
;