views:

77

answers:

3

so, instead of lots of instances of

if (odbc_exec($sql))
{
}
else
{
  myErrorHandlingFunction();
}

I wrap that in a function

function myOdbxExec($sql)
{
  if (odbc_exec($sql))
  {
  }
  else
  {
    myErrorHandlingFunction();
  }
}

BUT I would like myErrorHandlingFunction() to report things like __LINE__ __FILE__ etc

Which looks like I have to pass thoses infos to every call of helper functions, e.g. myOdbxExec($sql, __FILE__, __LINE__) which makes my code look messy.

function myErrorHandlingFunction($errorTExt, $fiel, $line)
{
  // error reporting code goes here
}


function myOdbxExec($sql, $file, $line)
{
  if (odbc_exec($sql))
  {
  }
  else
  {
    myErrorHandlingFunction();
  }
}

$sql = 'select * from ... blah, blah, blah...';
myOdbxExec($sql, __FILE__, __LINE__);   // <==== this is *ugly*

In C I would hide it all behind a #define, e.g. #define MY_OFBC_EXEC(sql) myOdbxExec(sql, __FILE__, __LINE__)

1) (how) can I do that in PHP 2) what else is worth outoputting? e.g. error_get_last()? but that has no meaning if odbc_exec() fails ...

To rephrase the question - what's the generic approach to PHP error handling? (especially when set_error_handler() doesn't really apply?


Edit: just to be clear - I do want to handle exceptions, programming errors, etc, but, as my example shows, I also want to handle soemthings the teh PHP interpreter might noit consider to be an error, like odbc_exec() returning false().

+1  A: 

Use debug_backtrace to figure out where a function was called from in your error handler. Whether you invoke this error handler by manually calling it, by throwing and catching exceptions, PHPs error_handler or any other method is up to the application design and doesn't really differ that much from other languages.

deceze
+1 Thanks. I editted the question to add this "just to be clear - I do want to handle exceptions, programming errors, etc, but, as my example shows, I also want to handle soemthings the teh PHP interpreter might noit consider to be an error, like `odbc_exec()` returning false()."
LeonixSolutions
+6  A: 

First off, you might want to consider using exception handling.

If for some reason, that doesn't appeal to you, you can use debug_backtrace inside your generic error handler to figure out where the handler was called from.

EDIT In response to OP's comment:

Exceptions don't have to come from PHP built-ins. You can throw your own. Since an ODBC error generally is an exceptional condition, just throw an exception when you detect one. (And catch it at some higher level).

<?PHP
if (! odbc_exec($sql) ) 
    throw new DatabaseException('odbc_exec returned false, that bastard!');
timdev
+1 Thanks. I editted the question to add this "just to be clear - I do want to handle exceptions, programming errors, etc, but, as my example shows, I also want to handle soemthings the the PHP interpreter might not consider to be an error, like `odbc_exec()` returning false()."
LeonixSolutions
+3  A: 

Both the above answers mentioning debug_backtrace are answering your __LINE__ / __FILE__ question with the debug_backtrace() function. I've wanted this answer too in the past, so have put together a quick function to handle it.

function myErrorHandlingFunction($message, $type=E_USER_NOTICE) {
    $backtrace = debug_backtrace();
    foreach($backtrace as $entry) {
        if ($entry['function'] == __FUNCTION__) {
            trigger_error($entry['file'] . '#' . $entry['line'] . ' ' . $message, $type);
            return true;
        }
    }
    return false;
}

You can then call, for instance, myErrorHandlingFunction('my error message');

or myErrorHandlingFunction('my fatal error message', E_USER_ERROR);

or try { ... } catch (Exception $e) { myErrorHandlingFunction($e->getMessage()); }

Colin
+1 That looks very good!! I'll give it a try and tell you how it goes. Thanks
LeonixSolutions