I’m working on a project at the moment. Doing stuff for company called Boogie Beat Oy. I’m doing a whole rewrite for their system-of-a-thingie… I decided to start using proper OOP-style. Pretty interesting thing to do with a language which I’ve used only for procedural programming. So I stumbled upon some of the problems which PHP4 has with objects and error handling.
set_error_handler(“yourErrorHandlerFunction”)
I’m used to do objects with Java andJava’s way of handling errors which is quite nice and flexible.
In PHP4 the built in way of raising errors is:
trigger_error("Error message", E_USER_ERROR)
This is not the actual problem but the:
function my_errror_handler($errno, $errstr, $errfile, $errline, $errcontext) {
switch($errno) {
case E_USER_ERROR:
...
}
}
$old_error_handler = set_error_handler("my_error_handler");
Which would be just brilliant if you could say…
$old_error_handler = set_error_handler("$object->errorHandler");
…and when you can’t. You’re basically bound to do like this:
require('Object.class.php');
$object = new Object();
function __errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
global $object;
$object->errorHandler($errno, $errstr, $errfile, $errline, $errcontext);
}
$oldErrorHandler = set_error_handler("__errorHandler");
Which is kind of sucky way of doing things… Basically what I’ve done is that my errorHandler will store all of the errors in an Array and I can just ask for the errors when I need to get them and it’ll return an Array of the errors. So I can easily foreach the errors in a Smarty template and not have to worry about the errors been print out anywhere or anytime they are triggered.
Took me some time to figure out a way to point to the function of this errorHandler class I wrote. This implementation is a hack but it works! It makes it easy to localize the errors if wanted because you can modify the errorHandling as you wish…
Here’s few documents where you can find more information about the error handling in PHP4
Oh well, that’s about it… Probably more about this later…