Trigger an error with trigger_error or Exception in a different scope

2

Assuming I have a function in a file called foo.php:

<?php
function foo() {
    trigger_error('Olá mundo!', E_USER_ERROR);
}

And in the index.php file I would use this:

<?php
require 'foo.php';

//Chamando erro
foo();

It will issue an error something like this:

  

Fatal error: Hello world! in /path/foo.php on line 3

However I would like to issue the trigger_error in the file that I called foo(); , for example, see that the path /path/foo.php on line 3 appears, but what I would like to get is the path in /path/index.php on line 5 since this line is the line where I called foo(); .

I know I can create a custom error using echo or printf for example, but the problem is that these custom errors do not affect error_get_last and error_clear_last .

I would like to know if it is possible to set a Exception for example the line and the scope of the error.

    
asked by anonymous 26.10.2015 / 20:35

1 answer

1

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

    
27.10.2015 / 02:27