Can't catch a ReflectionException?

I have this code:

namespace Some\Different\Name; try { $reflect = new ReflectionClass($class); X: $instance = $reflect->newInstanceArgs($args); } catch (ReflectionException $e) { exit($e->getMessage()); } 

and I'm testing it trying to throw a ReflectionException. And it gives me:

Fatal error: throw a "ReflectionException" exception with the message "MyClass has no constructor, so you cannot pass any constructor arguments' ... on line X.

What am I doing wrong?

PS: I know why the exception is thrown, I just want to know why he didn't catch!

+6
source share
1 answer

At last. This was a namespace problem. It is strange that PHP does not report that you are trying to catch an exception of type ( ReflectionException ) that does not exist in the current namespace.

Just adding \ to \ReflectionException helped me, because now it is able to find what type of exception I'm really looking for.

Another solution would be to add:

 use \ReflectionException; 

immediately after the namespace declaration.

+6
source

Source: https://habr.com/ru/post/909323/


All Articles