Skip to content

Instantly share code, notes, and snippets.

@phpdave
Last active February 3, 2016 03:53
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 phpdave/4e1c69a4a5918f786ce2 to your computer and use it in GitHub Desktop.
Save phpdave/4e1c69a4a5918f786ce2 to your computer and use it in GitHub Desktop.
<?php
function foo(): bool {
return null;
}
try {
var_dump(foo());
}
catch (Throwable $t) {
// Executed only in PHP 7, catches errors and exceptions
echo "Throwable: {$t->getMessage()}\n";
}
catch (Exception $e) {
// Executed PHP7 and 5.x, catches exceptions
// ::NOTE:: Redundant if your catching throwable in PHP7
echo "Exception: {$e->getMessage()}\n";
}
catch (Error $e){
//Executed only in PHP 7, catches errors
//Type mismatch is a subclass of Error (TypeError)
//http://php.net/manual/en/class.typeerror.php
// ::NOTE:: Redundant if your catching throwable in PHP7
}
//OUTPUT: Throwable: Return value of foo() must be of the type boolean, null returned
//NOTE: If we were to cast null as a boolean i.e. :
//return (bool) null;
//we would get false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment