I found a great example in SOen that lets you in some way use to inform which scope the custom method was called .
You will need to use $trace = debug_backtrace(FALSE);
to let you know where your method was called.
The foo.php should look something like:
<?php
function foo() {
//Pega o escopo de quem chamou foo();
$trace = debug_backtrace(false);
$message = 'Olá mundo! ' .
sprintf('in %s on line %d Triggered', $trace[0]['file'], $trace[0]['line']);
$trace = NULL;
trigger_error($message, E_USER_NOTICE);
}
When running index.php:
<?php
require 'foo.php';
//Chamando
foo();
echo '<pre>';
print_r(error_get_last());
echo '</pre>';
You will get the error on the screen and you can still use error_get_last()
(this will vary from what type of error you are using in trigger_error
or if you are going to capture with set_handler_error
), the output will look something like this:
Notice: Hello world! in /path/index.php on line 5 Triggered in /path/foo.php on line 11
Array
(
[type] = > 1024
[message] = > Hello World! in /path/index.php on line 5 Triggered
[file] = > /path/foo.php
[line] = > 11
)
Note that in error_get_last
the line the file you entered is foo.php, but you can parse the message
if you need it.
The idea of creating a custom error like this is to be able to create own functions and if there is a failure the script could tell you exactly where the method was executed, for example:
function dividir($a, $b) {
$err = NULL;
if (false === is_numeric($a)) {
$err = 'Primeiro argumento é invalido';
} else if (false === is_numeric($b)) {
$err = 'Segundo argumento é invalido';
} else if ($b == 0) {
$err = 'Não pode dividir por zero';
}
if ($err) {
$trace = debug_backtrace(false);
$message = $err .
sprintf('in %s on line %d Triggered', $trace[0]['file'], $trace[0]['line']);
$trace = NULL;
trigger_error($message, E_USER_NOTICE);
return NULL;
}
return $a / $b;
}
In this case we have 3 possible errors, so if we use the method like that in index.php:
<?php
require 'foo.php';
echo dividir(10, 0);
Let's have the following error:
Notice: You can not divide by zero in index.php on line 4 Triggered in foo.php on line 20
Some details on this other answer on debug_backtrace: link