Last active
August 29, 2015 13:56
-
-
Save mrvaldes/8909183 to your computer and use it in GitHub Desktop.
PHP test of exceptions behavior in closures
This file contains hidden or 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 | |
| echo "php version: ".phpversion()."\n"; | |
| echo "\ncall named function:\n---\n"; | |
| function named() { | |
| try{ | |
| echo "try\n";return true; | |
| } finally { | |
| echo "finally\n";return false; | |
| } | |
| } | |
| if (named()) { | |
| echo "true\n"; | |
| } else { | |
| echo "false\n"; | |
| } | |
| echo "\ncall anon. function:\n---\n"; | |
| if (function(){ | |
| try{ | |
| echo "try\n";return true; | |
| } finally { | |
| echo "finally\n";return false; | |
| } }) { | |
| echo "true\n"; | |
| } else { | |
| echo "false\n"; | |
| } | |
| echo "\nanon. function value to variable:\n---\n"; | |
| $foo = function() { | |
| try{ | |
| echo "try\n";return true; | |
| } finally { | |
| echo "finally\n";return false; | |
| } | |
| }; | |
| if ($foo) { | |
| echo "true\n"; | |
| } else { | |
| echo "false\n"; | |
| } | |
| var_dump($foo); | |
| echo "\ncall anon. function from variable:\n---\n"; | |
| $bar = function() { | |
| try{ | |
| echo "try\n";return true; | |
| } finally { | |
| echo "finally\n";return false; | |
| } | |
| }; | |
| if ($bar()) { | |
| echo "true\n"; | |
| } else { | |
| echo "false\n"; | |
| } | |
| var_dump($bar); | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php version: 5.5.8-3
call named function:
try
finally
false
call anon. function:
true
anon. function value to variable:
true
object(Closure)#1 (0) {
}
call anon. function from variable:
try
finally
false
object(Closure)#2 (0) {
}