Back to questions
JuniorTheoryCommon
What does the program print?
<?php
namespace App;
try {
throw new \RuntimeException('x');
} catch (Exception $e) {
echo 'app';
} catch (\Exception $e) {
echo 'global';
}
echo "\n";global
- Expecting
catch (Exception $e)to catch it inside a namespace: a class name without\is looked up in the current namespace, so it meansApp\Exception, which matches nothing. - Expecting a
catchfor a missing class to raise an error: PHP never looks that class up, the block silently never matches, and the mistake goes unnoticed.
- How do you fix the first
catchso that it catches any exception? - Why does
strlen()work insidenamespace Appwithout a leading backslash?