Created
May 15, 2012 20:31
-
-
Save madfriend/2704897 to your computer and use it in GitHub Desktop.
How to treat the most stupid thing in PHP
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 | |
// 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