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“.
I suppose a solution could be to use
debug_backtrace.The given example gets a backtrace like this :
So, should include what you want 😉
And if you just want to output the trace (not likely), there is also
debug_print_backtrace.