I'm looking for a strategy to do informative error handling at all
levels of my code, yet keep these errors non-fatal as often as
Adhering to a proper code-standard with regards to error handling
would speed up development of any code (it would shorten the bug-hunt
time a lot), but it becomes especially usefull for code that processes
items in large diverse datasets; exceptions should be logged (in
different ways) but should not kill off the processing of other items.

I have some experience with set_error_handler() and trigger_error(),
but haven't perfected my use of them yet.

Take for instance the following function;

function chase ($arr, $indexes) {
  $r = $arr;
  foreach ($indexes as $idx) {
    if (is_array($r)) {
      $r = $r[$idx];
    } else {
      trigger_error (htmlDumpReturn (
        array (
            'arr' => $arr,
            'indexes' => $indexes
        ), 'chase() could not walk the full tree.'
      ), E_USER_WARNING);
      return false;
  return $r;

This is a low-level function used by me to traverse to a value several
arbitrary levels into an array.
As you can see, things will be fine for any value that is not
(boolean)false, but if i'm ever to fetch a "false" value, things will

The solution i can think of is to have all my functions return not
their results directly, but 1 of these arrays instead:
//good result:
return array (
  'result' => $valueFound
//no result / bad result:
return array (
  'error' => 'same error details as passed to trigger_error()'
While this would allow the calling code to fudge, redirect or omit the
failed-item, i have a small fear that this approach leads to
"complicated" code whenever a function is called, but I suppose that
with a few extra short-named functions ( like is_error() ) the calling
code can be kept short and simple too.

(local) log-levels and (also local) redirection-of-error-messages i
can solve with global variables and some functions to manipulate

My question to you all is; do you know a simpler way of achieving what
i'm aiming for?

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to