debug_backtrace()函數是PHP中的一個內置函數,一般供程序員調試使用。 debug_backtrace() 函數的主要工作是生成 PHP 回溯,即檢查堆棧跟蹤。它返回一個回溯 PHP 代碼的關聯數組的數組。此函數所有可能返回的元素是 - Name Type Descriptionfunction string 正在調用 debug_backtrace() 函數的函數的名稱。 line integer 函數調用的當前行號。 file string 已調用 debug_backtrace() 函數的文件的名稱。 class string 調用 debug_backtrace() 函數的類的名稱。 object object 已用於調用存在 debug_backtrace() 函數的成員函數的對象的名稱。 type string 當前調用的類型。如果當前調用類型是方法調用,則返回 “->”。如果是靜態方法調用,則返回 “::”,如果是函數調用,則不返回任何內容。 args array 函數定義中存在的參數列表。如果在文件中使用了 debug_backtrace() 函數,則返回其中包含的所有文件。
用法:
array debug_backtrace(int $options, int $limit);
這裏,參數 $options 和 $limit 都是可選的並且是整數類型。 $option 參數用於位掩碼,$limit 可用於設置限製要打印的堆棧幀的數量。 $limit 的默認值設置為零。
例:在此示例中,我們創建了一個返回兩個輸入參數之和的函數,並且在其中使用了 debug_backtrace() 函數。在首先打印輸出時,將顯示輸入參數的總和,然後將打印代碼的回溯。
PHP
<?php
function sum($a, $b) {
echo $a + $b . "\n\n";
var_dump(debug_backtrace());
}
sum(10,2);
?>
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
<?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();
?>
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/function.debug-backtrace.php
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- PHP fpassthru( )用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP array_intersect_ukey()用法及代碼示例
- PHP is_numeric()用法及代碼示例
- PHP Imagick adaptiveSharpenImage()用法及代碼示例
- PHP XMLWriter endDtdEntity()用法及代碼示例
- PHP isset()用法及代碼示例
注:本文由純淨天空篩選整理自talktoosaurabh大神的英文原創作品 PHP debug_backtrace() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。