PHP Exception Handling - ErrorException

PHP Exception Handling – ReflectionException

Moving along through the encompassing PHP Exception Handling series we’ve been creating, today we’ll be taking a look at the PHP ReflectionException. A ReflectionException occurs when there’s an error while performing any sort of reflection; specifically when dealing with the Reflector, or with other classes that inherit from it.

In this article we’ll explore the ReflectionException by looking at where it sits in the overall PHP Exception Hierarchy. We’ll also examine some functional PHP code samples that illustrate how such errors might be thrown, so you can see how they should be handled. Let’s get going!

The Technical Rundown

All PHP errors implement the Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. Feel free to use any or all of the code if you wish to follow along.

When Should You Use It?

In programming, reflection is the means by which existing classes and objects can be “reverse-engineered”, allowing runtime investigations and use of classes, functions, methods, and so forth, without any prior knowledge of how those objects work or are coded. We won’t go into much more depth on what reflection is or how it works in this article, but have a look here if you’re interested in learning more.

Since PHP 5, most reflection takes place within the ReflectionClass, so that’s what we’ll be using today. To illustrate how it works, and how we might also run into a ReflectionException, we start with our basic Book class:

To test things out, we start by making a direct, explicit call to the Book constructor, and output the resulting instance to the log:

Unsurprisingly, this works just as expected:

Now, let’s try creating an instance of ReflectionClass, in which we pass the string name of the class we want to reflect, Book:

If we get a result back and don’t produce an error, we’re in business. Sure enough, that’s exactly what our output shows:

With the $reflection variable holding our ReflectionClass instance of the Book class, we can now call some of its built-in methods, just to illustrate the basic functionality and power of the ReflectionClass. Here we’re getting a list of all the constants and methods within the reflected Book class:

And here’s the produced output:

Finally, we can even use our ReflectionClass instance to create a new instance of the reflected Book class by calling the newInstance() method:

The output should be of a Book instance, just like at the top when we explicitly invoked new Book(...):

That’s pretty cool, but let’s now see what happens if we try to use ReflectionClass improperly. For example, here we’re trying to construct a new instance while passing the name of an invalid class:

Lo and behold, ReflectionClass cannot find a class named Invalid, so it throws a ReflectionException our way as a result:

This is just a small taste of what the ExceptionReflection class can do, but hopefully it gives you a jumping-off point to use it in your own projects!.

Check out the Airbrake-PHP library, designed to quickly and easily integrate into any PHP project, giving you and your team access to real-time error monitoring and reporting throughout your application’s entire life cycle. With automatic, instantaneous error and exception notifications at your fingertips, you’ll be constantly aware of your application’s health, including any issues that may arise. Best of all, with Airbrake’s robust web dashboard cataloging every error that occurs, you and your team can immediately dive into the exact details of what went wrong, making it easy to quickly recognize and resolve problems.