คำอธิบาย
ฟังก์ชัน debug_backtrace() สร้าง backtrace (การติดตามย้อนกลับ) และคืนค่าเป็นอาร์เรย์ associative
รูปแบบการใช้งาน (Syntax)
debug_backtrace(options, limit)
พารามิเตอร์
- options — บิตมาสก์:
DEBUG_BACKTRACE_PROVIDE_OBJECT— รวมดัชนี "object"DEBUG_BACKTRACE_IGNORE_ARGS— ไม่รวมดัชนี "args" เพื่อประหยัดหน่วยความจำ
- limit — จำกัดจำนวนเฟรมสแต็ก
โครงสร้างอาร์เรย์ที่คืนกลับ
| คีย์ | ประเภท | คำอธิบาย |
|---|---|---|
| function | string | ชื่อฟังก์ชันปัจจุบัน |
| line | integer | หมายเลขบรรทัดปัจจุบัน |
| file | string | ชื่อไฟล์ปัจจุบัน |
| class | string | ชื่อคลาสปัจจุบัน |
| object | object | ออบเจ็กต์ปัจจุบัน |
| type | string | ประเภทการเรียก: "->" (เมธอด), "::" (static), "" (ฟังก์ชัน) |
| args | array | อาร์กิวเมนต์ของฟังก์ชัน |
ตัวอย่างการใช้งาน
<?php
function display($str) {
echo "Hi: $str\n";
var_dump(debug_backtrace());
}
display('hello');
?>
ผลลัพธ์
Hi: hello
array(1) {
[0]=>
array(4) {
["file"]=> string(36) "/path/to/script.php"
["line"]=> int(8)
["function"]=> string(8) "display"
["args"]=> array(1) {
[0]=> &string(5) "hello"
}
}
}