名字 | 類型 | 說明 |
---|---|---|
function | string | 當前的函數名,參見: __FUNCTION__。 |
line | integer | 當前的行號。參見: __LINE__。 |
file | string | 當前的文件名。參見: __FILE__。 |
class | string | 當前 class 的名稱。參見 __CLASS__ |
object | object | 當前的 object。 |
type | string | 當前調用的類型。如果是一個方法,會返回 "->"。如果是一個靜態方法,會返回 "::"。 如果是一個函數調用,則返回空。 |
args | array | 如果在一個函數里,這會列出函數的參數。 如果是在一個被包含的文件里,會列出包含的文件名。 |
獲取訂單的用戶資料及用戶訊息,調用流程是index->order->user->message,最后返回整理后的信息。
假設我們調試時發現message的數據有誤,則可以在message使用debug_backtrace
方法,查看調用的流程及調用的參數,檢查哪一步出現問題。
使用DEBUG_BACKTRACE_IGNORE_ARGS
則會忽略args(方法調用的參數)
index.php
?php require 'order.php'; // 獲取用戶訂單資料 $order_id = 1000000; $oOrder = new Order; $order_info = $oOrder->get_order($order_id); ?>
order.php
?php require 'user.php'; // 訂單資料 class Order{ // 獲取訂單資料 function get_order($order_id){ $user_id = 1001; // 獲取用戶資料 $oUser = new User; $user_info = $oUser->get_user($user_id); // 訂單資料 $order_info = array( 'order_id' => $order_id, 'order_name' => 'my order', 'user_info' => $user_info, ); return $order_info; } } ?>
user.php
?php require 'message.php'; // 用戶資料 class User{ // 獲取用戶資料 function get_user($user_id){ // 獲取用戶訊息 $oMessage = new Message; $user_message = $oMessage->get_message($user_id); $user_info = array( 'user_id' => $user_id, 'name' => 'fdipzone', 'message' => $user_message ); return $user_info; } } ?>
message.php
?php // 用戶訊息 class Message{ // 獲取用戶訊息 function get_message($user_id){ $message = array( array('id'=>1, 'title'=>'message1'), array('id'=>2, 'title'=>'message2'), ); // 加入跟蹤調試 $backtrace = debug_backtrace(); var_dump($backtrace); return $message; } } ?>
運行index.php, 輸出
/message.php:15:
array (size=3)
0 =>
array (size=7)
'file' => string '/user.php' (length=9)
'line' => int 12
'function' => string 'get_message' (length=11)
'class' => string 'Message' (length=7)
'object' =>
object(Message)[3]
'type' => string '->' (length=2)
'args' =>
array (size=1)
0 => int 1001
1 =>
array (size=7)
'file' => string '/order.php' (length=10)
'line' => int 14
'function' => string 'get_user' (length=8)
'class' => string 'User' (length=4)
'object' =>
object(User)[2]
'type' => string '->' (length=2)
'args' =>
array (size=1)
0 => int 1001
2 =>
array (size=7)
'file' => string '/index.php' (length=9)
'line' => int 8
'function' => string 'get_order' (length=9)
'class' => string 'Order' (length=5)
'object' =>
object(Order)[1]
'type' => string '->' (length=2)
'args' =>
array (size=1)
0 => int 1000000
可以看到調用過程是
1.index.php
line 8
class Order
function get_order
args int 10000002.order.php
line 14
class User
function get_user
args int 10013.user.php
line 12
class Message
function get_message
args int 1001
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP錯誤與異常處理方法總結》、《php字符串(string)用法總結》、《PHP數組(Array)操作技巧大全》、《PHP運算與運算符用法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
下一篇:php 可變函數使用小結