0

I have the following function:

function backtrace($Object=false)
{
    $x = 0;
    foreach((array)debug_backtrace($Object) as $aVal)
    {
        $row[$x]['file']     = $aVal['file'];
        $row[$x]['line']     = $aVal['line'];
        $row[$x]['function'] = $aVal['function'];
        $row[$x]['class']    = $aVal['class'];
        $row[$x]['args']     = $aVal['args'];
        ++$x;
    }
    return $row;
}

But when I use it, I'm getting an error like below:

Warning: debug_backtrace() expects parameter 1 to be long, string given in /mypath/ on line 717 ---> foreach((array)debug_backtrace($Object) as $aVal)

What's causing the error? How can I fix it?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • did you read manual: http://pl1.php.net/debug_backtrace ? from 5.3.6 first parameter is bitmask. – ziollek Mar 04 '14 at 19:15

2 Answers2

1

The first parameter of debug_backtrace() is a bitmask of options (i.e. a long). It is a simple boolean true/false in PHP versions prior to 5.3.6.

To fix it, either don't pass in the $Object variable you're currently passing in or update it to be any combination of the supported options that you want to be used.

Example:

$Object = DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT;

If you want to add a pre-condition to your current block of code that will set a default value if $Object is invalid, you could try something like:

function backtrace($Object = false) {
    if (!is_long($Object) || (!($Object & DEBUG_BACKTRACE_PROVIDE_OBJECT) && !($Object & DEBUG_BACKTRACE_IGNORE_ARGS))) {
        $Object = 0;
    }

    $x = 0;
    foreach((array)debug_backtrace($Object) as $aVal) {
        $row[$x]['file']     = $aVal['file'];
        $row[$x]['line']     = $aVal['line'];
        $row[$x]['function'] = $aVal['function'];
        $row[$x]['class']    = $aVal['class'];
        $row[$x]['args']     = $aVal['args'];
        ++$x;
    }
    return $row;
}
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

for php >= 5.3.6, you should use bitmask options

function backtrace($Object=false) {
    $x = 0;
    foreach((array)debug_backtrace($Object ? DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) as $aVal)
    {
        $row[$x]['file']     = $aVal['file'];
        $row[$x]['line']     = $aVal['line'];
        $row[$x]['function'] = $aVal['function'];
        $row[$x]['class']    = $aVal['class'];
        $row[$x]['args']     = $aVal['args'];
        ++$x;
    }
    return $row;
}
ziollek
  • 1,973
  • 1
  • 12
  • 10