Created
October 19, 2011 13:26
-
-
Save Reinmar/1298274 to your computer and use it in GitHub Desktop.
PHP sucks
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 | |
namespace x; | |
require 'catch_me2.php'; | |
error_reporting(E_ALL | E_STRICT); | |
ini_set('display_errors', 1); | |
try { | |
\y\shittyCode(); | |
} | |
catch (Exception $e) { | |
echo 'Gotcha!'; | |
} | |
//Guess what? Uncaught exception 'Exception' with message 'catch me if you can' :) | |
//No error caused by lack of Exception class in this namespace |
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 | |
namespace y; | |
function shittyCode() { | |
throw new \Exception('catch me if you can'); | |
} | |
I know what's the difference between \Exception and Exception, and already fixed that in my script. But the problem is with lack of information - there's silent failure, although in namespace x there's no Exception class.
You can show that without second file - that was suggesting something different. Just throw new \Exception and works the same. But you're right, there's no info about not defined exception class, and I agree it should be one. It's mentioned in manual.
I know I can, but then it wouldn't be so tricky :> In the case I was working on the exception was thrown far far away, so it took me a while before I've found wtf.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are throwing
\Exception
which is base exception class from PHP standard library. You're catching Exception from namespace x. ReplaceException
with\Exception
in catch_me.php at line 11 and everything works as it should.And please change snippet name to "Reinmar can't read manual." :P