Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Ole Markus' Blog:
Catching fatal errors in PHP
Mar 11, 2011 @ 15:16:42

Ole Markus has a new post today looking at how you can catch fatal errors in your PHP applications a bit more gracefully than the usual failure messages.

In dynamic languages like PHP [errors like E_ERROR and E_PARSE] happen all the time, for example when trying to call a method on a variable you assumed was an instance of a specific class, but which for some reason suddenly was not instantiated. Not only are they often not catched, but often it is also difficult to even know that they are occurring.

His solution comes in the form of a built-in PHP function, register_shutdown_function, that executes when the PHP process is shutting down - errors or not. It takes in a callback method that has access to an exception object. You can get lots of interesting information from this object and, as in his example, log it to a file for future investigation.

tagged: catch fatal error registershutdownfunction exception zendlog

Link:

Eirik Hoem's Blog:
Dying with grace - PHP's register_shutdown_function
Mar 17, 2008 @ 17:02:00

Eirik Hoem has posted a new look at a function that can be amazingly helpful when you have a script with issues that needs a little extra help cleaning up after itself - register_shutdown_function.

Scripts tend to die, and that's not usually nice. We do not want to show the user a fatal error nor a blank page (display errors off) . PHP has a function called register_shutdown_function which lets us set up a function which is called at execution shutdown. What this means is that our function will be executed when our script is done executing / dying and PHP execution is about to shut down.

He suggests various things that can be done with the functionality, including checking for successful script execution (via a false variable that can be checked for success).

tagged: registershutdownfunction shutdown function register die success

Link:


Trending Topics: