I hope the title is not too confusing, I will try to explain below.
Suppose I have a function in a separate file, functions.php:
function divide($num1, $num2) { if ($num1 == 0 || $num2 == 0) { trigger_error("Cannot divide by 0", E_USER_ERROR); } else { return ($num1 / $num2); } }
And another file that calls it:
include "functions.php"; echo divide(10, 0);
My mistake
Fatal error: cannot divide by 0 in C: \ Users \ Derek \ Desktop \ projects \ functions.php on line 5
My question is: how can I make this error, instead I will indicate the location of the error in the main code, so I get instead:
Fatal error: cannot divide by 0 in C: \ Users \ Derek \ Desktop \ projects \ main.php on line 3
The specific reason I want this is because I have a load_class function that simply finds the PHP file and creates an instance of the object inside, but if it is given the wrong file name, it reports an error from within load_class, which is technically correct but this is not particularly useful if I don’t remember where I called load_class in the first place. I would like the error to point to a file called load_class.
In addition, I would like to write the error () function (something like below) that, when a message is provided as a parameter, more "meaningful" error messages will be generated, but when this is done, the error always says that it comes from error (), not where the error came from!
For example, in the error.php file:
function error($type, $message) { switch (strtolower($type)) { case 'fatal': trigger_error($message, E_USER_ERROR); break; case 'notice': trigger_error($message, E_USER_NOTICE); default: trigger_error($message, E_USER_WARNING); break; } }
And in index.php
error("fatal", "A sample warning!");
My mistake:
Fatal error: selective warning! in C: \ Users \ Derek \ Desktop \ projects \ summary \ sys \ Error.php on line 45
But the error did not happen in error.php, it happened in index.php! How can I show where it came from?