views:

49

answers:

1

In PHP, I'd like to get the details about a function call inside the function itself. The behavior I want (without doing this) is to have debug_backtrace() passed as an argument to the function.

I want that done automatically, for every call to a function.

I need this so I can have pre-defined errors for a fairly sizable project I'm working on, but I obviously want the line number of a central trigger_error() call, as that's not very useful for tracking down the problem. I also don't want to count on future developers to remember a debug_backtrace() argument.

+2  A: 

You do realize that you can call debug_backtrace() yourself, and it would be pretty much the same. For instance:

function error(..params)
{
    $backtrace = debug_backtrace();
    array_shift($backtrace);
}

If you array_shift the given backtrace, if will be as if it were passed into the function.

If you aren't familiar with it:

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.

Thus, because debug_backtrace() is numerically indexed, it will act the exact same.

Chacha102
It wouldn't be the same, as I need the details in the context of where the function was called, not where the function is defined.
Shadow
`debug_backtrace()` doesn't really do anything in the ways of where a function was defined. Either you aren't clearly communicating something, or you don't understand how it works...
Chacha102
@Shadow Are you asking for a snapshot of the scope variables when a function is called and subsequently invokes a `trigger_error()`?
Mike B
My appologies. I didn't realize that debug_backtrace contained the *full* backtrace. Probably the first time I've ever said this, but the PHP documentation doesn't communicate that very well.
Shadow
@Shadow I tend to `print_r` almost every array I ever use that comes from PHP, just to make sure I know what is in it.
Chacha102