Skip to content

Instantly share code, notes, and snippets.

@ameenross
Last active December 20, 2018 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameenross/a6f17555156d579222cb4e0a99623670 to your computer and use it in GitHub Desktop.
Save ameenross/a6f17555156d579222cb4e0a99623670 to your computer and use it in GitHub Desktop.
PHP 7.2 suppress warning from count
<?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);
@ameenross
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment