Skip to content

Instantly share code, notes, and snippets.

@madfriend
Created May 15, 2012 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madfriend/2704897 to your computer and use it in GitHub Desktop.
Save madfriend/2704897 to your computer and use it in GitHub Desktop.
How to treat the most stupid thing in PHP
<?php
// Assume this function definition:
function foo($bar, $baz = 'qux') {
return $bar.$baz;
}
// Oh crap, something went wrong, and we call it without arguments:
$foo = foo();
// Produces:
// PHP Warning: Missing argument 1 for foo() bla bla bla
// PHP Notice: Undefined variable: bar in bla bla bla
// And script doesn't terminate. That's really stupid.
// Here is a simple solution:
set_error_handler(function($errno, $errstr) {
if (preg_match('/Missing argument/', $errstr)==1)
trigger_error('Missing argument causes fatal error, mothafucka!', E_USER_ERROR);
}, E_WARNING);
$foo = foo();
// PHP Fatal error: Missing argument causes fatal error, mothafucka! in bla bla bla
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment