Open In App

PHP debug_backtrace() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The debug_backtrace() function is an inbuild function in PHP which is generally used by programmers in debugging. The main work of debug_backtrace() function is to generate the PHP backtrace i.e. to examine the stack trace. It returns an array of associate array of the backtrace PHP code. All possible returned elements by this function are –

Name

Type

Description

function string Name of the function in which debug_backtrace() function is being called.
line integer Current line number of the function call.
file string Name of the file where debug_backtrace() function has been called.
class string Name of the class where debug_backtrace() function has been called.
object object Name of the object which has been used to evoke the member function in which debug_backtrace() function is present.
type string The type of current call . If the current call type is method call then , “->” is returned. If it is a static method call then, “::” is returned and If it is a function call then, nothing is returned.
args array List of the Arguments present in the function definition. If the debug_backtrace() function is used within the file then all the files included within that is returned. 

Syntax:

array debug_backtrace(int $options, int $limit);

Here, both the parameters $options and $limit are optional and are of integer type. The $option parameter is used for bitmask and $limit can be used to set limit the number of stack frame to be printed. Default value for the $limit is set to zero.

Example: In this example, we have created a function that returns the sum of two input parameters and inside that debug_backtrace() function has been used. While printing the output first the sum of the input parameters will be displayed and then the backtrace of the code will be printed. 

PHP




<?php
    
function sum($a, $b) {
    echo $a + $b . "\n\n";
      
    var_dump(debug_backtrace());
}
  
sum(10,2);
  
?>


Output

12

array(1) {
  [0]=>
  array(4) {
    ["file"]=>
    string(42) "/home/2228b7c9e401174a5f773007cd840e32.php"
    ["line"]=>
    int(9)
    ["function"]=>
    string(3) "sum"
    ["args"]=>
    array(2) {
      [0]=>
      int(10)
      [1]=>
      int(2)
    }
  }
}

Example 2: In this example, the concept of debug_backtrace() function has been implemented within the class along with the concept of recursion to check it’s back trace. In this example, two class i.e. BaseClass and DerivedClass has been created along with their constructors and inside the constructor of BaseClass debug_backtrace() has been called. The output generated i.e. back trace for this code consist of all the elements mentioned in the above table accordingly.

PHP




<?php
  
class BaseClass {
    public function __construct() {
        $this->_child = new DerivedClass($this);
        var_dump(debug_backtrace());
    }
}
  
class DerivedClass {
    public function __construct(BaseClass $d) {
        $this->_parent = $d;
    }
}
  
$obj = new BaseClass();
?>


Output

array(1) {
  [0]=>
  array(7) {
    ["file"]=>
    string(42) "/home/ecdb752d3b6ec8ba97e6db84c42a5f2f.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(9) "BaseClass"
    ["object"]=>
    object(BaseClass)#1 (1) {
      ["_child"]=>
      object(DerivedClass)#2 (1) {
        ["_parent"]=>
        *RECURSION*
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}

Reference: https://www.php.net/manual/en/function.debug-backtrace.php



Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads