Last active
February 3, 2016 03:53
-
-
Save phpdave/4e1c69a4a5918f786ce2 to your computer and use it in GitHub Desktop.
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 | |
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