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 make a ReflectionException be thrown. And it gives me:
Fatal error: Uncaught exception ‘ReflectionException’ with message
‘Class MyClass does not have a constructor, so you cannot pass any
constructor arguments’ in … on line X.
What am I doing wrong?
PS: I know why the exception is thrown, I just wanna know why it’s not caught!
Finally. It was a namespace problem. It’s strange by the way that PHP doesn’t notify that you are trying to catch an exception of a type (
ReflectionException) that doesn’t exists in the current namespace.Just adding the
\to\ReflectionExceptionhelped me out because now it’s able to find what type of exception I’m actually looking for.Another solution would be to add:
just after the namespace declaration.