📜  PHP debug_backtrace()函数

📅  最后修改于: 2022-05-13 01:56:54.090000             🧑  作者: Mango

PHP debug_backtrace()函数

debug_backtrace()函数函数PHP中的一个内置函数,通常由程序员在调试时使用。 debug_backtrace()函数的主要工作是生成PHP回溯,即检查堆栈跟踪。它返回一个回溯PHP代码的关联数组的数组。此函数所有可能返回的元素是 -

Name

Type

Description

functionstringName of the function in which debug_backtrace() function is being called.
lineintegerCurrent line number of the function call.
filestringName of the file where debug_backtrace() function has been called.
classstringName of the class where debug_backtrace() function has been called.
objectobjectName of the object which has been used to evoke the member function in which debug_backtrace() function is present.
typestringThe 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.
argsarrayList 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. 

句法:

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

这里,参数$options$limit都是可选的并且是整数类型。 $option参数用于位掩码, $limit可用于设置限制要打印的堆栈帧的数量。 $limit 的默认值设置为零。

示例:在此示例中,我们创建了一个函数,该函数返回两个输入参数之和,并且在其中使用了 debug_backtrace()函数。在首先打印输出时,将显示输入参数的总和,然后将打印代码的回溯。

PHP


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


输出
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)
    }
  }
}

示例 2:在这个示例中,在类中实现了 debug_backtrace()函数的概念以及用于检查其回溯的递归概念。在这个例子中,已经创建了两个类,即 BaseClass 和 DerivedClass 以及它们的构造函数,并且在 BaseClass 的构造函数中调用了 debug_backtrace()。生成的输出,即此代码的回溯,相应地包含上表中提到的所有元素。

PHP

_child = new DerivedClass($this);
        var_dump(debug_backtrace());
    }
}
  
class DerivedClass {
    public function __construct(BaseClass $d) {
        $this->_parent = $d;
    }
}
  
$obj = new BaseClass();
?>
输出
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) {
    }
  }
}

参考: https://www. PHP.net/manual/en/函数.debug-backtrace。 PHP