Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน debug_backtrace() ใน PHP

คำอธิบาย

ฟังก์ชัน debug_backtrace() สร้าง backtrace (การติดตามย้อนกลับ) และคืนค่าเป็นอาร์เรย์ associative

รูปแบบการใช้งาน (Syntax)

debug_backtrace(options, limit)

พารามิเตอร์

  • options — บิตมาสก์:
    • DEBUG_BACKTRACE_PROVIDE_OBJECT — รวมดัชนี "object"
    • DEBUG_BACKTRACE_IGNORE_ARGS — ไม่รวมดัชนี "args" เพื่อประหยัดหน่วยความจำ
  • limit — จำกัดจำนวนเฟรมสแต็ก

โครงสร้างอาร์เรย์ที่คืนกลับ

คีย์ประเภทคำอธิบาย
functionstringชื่อฟังก์ชันปัจจุบัน
lineintegerหมายเลขบรรทัดปัจจุบัน
filestringชื่อไฟล์ปัจจุบัน
classstringชื่อคลาสปัจจุบัน
objectobjectออบเจ็กต์ปัจจุบัน
typestringประเภทการเรียก: "->" (เมธอด), "::" (static), "" (ฟังก์ชัน)
argsarrayอาร์กิวเมนต์ของฟังก์ชัน

ตัวอย่างการใช้งาน

<?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"
    }
  }
}