vendredi 26 août 2016

PHP: ReflectionMethod::invokeArgs() - Incomplete exception trace

I use PHP's ReflectionMethod::invokeArgs() to call a static class method.

The called method may throw exceptions at different positions in the code.

When an exception is thrown, the trace of the exception object ends with the name of the called method and I don't know at which line the exception was actually thrown.

Example:

class A
{
    public static function methA()
    {
        $RC = new ReflectionClass('A');
        $M  = $RC->getMethod('methB');

        return $M->invokeArgs(null, ['arg']);
    }

    public static function methB($arg)
    {
        //some code
        throw new Exception('Exception thrown');
    }
}

try
{
    A::methA();
}
catch(Exception $e)
{
    print_r($e->getTraceAsString()[0]);
}

Output:

Array
(
    [function] => methB
    [class] => A
    [type] => ::
    [args] => Array
        (
            [0] => arg
        )

)

What I miss is the file / line where the Exception was thrown. Instead the last entry contains just the name of the method.

Is there any other way to get that information without providing it via the exception message?





Aucun commentaire:

Enregistrer un commentaire