tags:

views:

50

answers:

2

I'm trying to build an error class and a lot of error classes I've looked at in the past use FILE and LINE to show where the error occurred, but they use them in the function so they're always the same, which is highly useless information. Aside from sending an array of containing FILE and LINE with every function (which would get to be quite a pain), I can't think of or find anything else. Is there a good way or a function out there to determine the correct line number and file that the function was being executed from?

Example:

<?php
// file1.php
dosomething(false);
?>

<?php
// file2.php
function dosomething($input) {
    if ($input === false) die("Error at Line (line) in File (file)");
}
?>

The function would die with the message "Error at Line 3 in File /file1.php".

Duplicate of Get code line and file that’s executing the current function in PHP?

A: 

Check set_error_handler function: http://us3.php.net/manual/en/function.set-error-handler.php You may also want to look on set_exception_handler function.

a1ex07
+2  A: 

Hi,

I suppose a solution could be to use debug_backtrace.

The given example gets a backtrace like this :

array(2) {
[0]=>
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    ["args"]=>
    array(1) {
      [0] => &string(6) "friend"
    }
}
[1]=>
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    }
    ["function"] => string(12) "include_once"
  }
}

So, should include what you want ;-)


And if you just want to output the trace (not likely), there is also debug_print_backtrace.

Pascal MARTIN