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 \ReflectionException
helped me out because now it's able to find what type of exception I'm actually looking for.
Another solution would be to add:
use \ReflectionException;
just after the namespace declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With