PHP Exception Handling

No one, including myself, seems like to write “try/catch” into their code. There is one reason why I don’t that stands out – an exception means that something went wrong with something vital enough that it cannot continue. I see a lot of PHP developers complain that they want to be able to ‘handle’ the exception by logging it or displaying it to the screen; however, if an exception occurs, it’s something vital enough that it was thrown to let you know of the error. Something in your code (or user input if you are using that) was wrong, and should be fixed – either in the code, or as a notice for the user.

I am currently working on my LW framework version 4. (It is called Liquid Web, as it has been since 2002, but since there is now a hosting company with that name, I’m not sure if I’ll run in to a trademark issue, as that’s something I never applied for.) With this, I am changing the purpose of LW from being a CMS to being a Framework for developer’s to use. Because of its nature, being dynamic with the classes/modules loaded, I had to make a ‘debug’ class that will record various actions (ie, ‘This class was loaded’, etc.). Now, those notices are stored within a class variable, and if something goes wrong, I want to be able to display the notices/warnings that have accumulated within the class before an error occurred. I also want the error to be displayed, and the application to quit so that as I develop the rest of the features for this framework, I can intuitively debug it depending on what went wrong, and where. After looking for hours on, and reading through all the comments, then some googling on the issue; I came up short for an answer of how to catch exceptions within a class, but not to have it statically called (I want my variables to be available). And so, I got to do some trial and error within my debug class – what i ended up with works like a charm.

First things, first, I have two private variables within the class:

The main variable used is $notices as that is where the “This class was loaded”/etc are stored. The $warnings is there for future implementation if I feel there’s a need to separate the two. To add a notice to the array, the following method handles:

Now, as previously said, there is no implementation to set warnings yet, though one exists to display them (so I have less code to change if I do use them eventually). Those two functions are:

I also have an error method to be given an error message, display all the warnings/notices along with the error, and then die.

Now, that’s all well and dandy, but anytime an exception happens, I want to work with it in the framework, not need to know the variable that is holding the debug object, and to display everything before it dies. I can do some of that with the error method, however, I will need to catch every exception, and then call the [debug variable]->error in every catch, or make a function that will handle it all, but still need to know the variable holding it.

After some trial and error, I ended up with a constructor method that looks like:

Now, obviously, due to how set_exception_handler works, I need a function, but using an array and passing the reference of the current class instance, I get full access to the notices/warnings that have already been stored. So after the constructor, I added the handleException method:

After that, ANY exception thrown within the code is passed to the debug exception handler, and I know what all got loaded before the error, the error is displayed at the top of the page with the file/line/etc.

Now, this entry isn’t meant to extensively cover how to handle exceptions in PHP, but it is meant to show a small demonstration of how to do it within a class structure so you can keep any other debugging information that may have already been passed to help further identify the problem in the code. I know that the debug class is simple, incredibly simple even, but it seems to fit its purpose of a basic logging/error handling class that is loaded into another class – and that is something I couldn’t find how to do in anything I read online. So I hope this may help any other programmers that have this problem.

No Comments Yet

There are no comments yet. You could be the first!

Leave a Comment

    Search the Blog