如何通過php 實現debug_backtrace方法跟蹤代碼調用

來源:互聯網
上載者:User

在開發過程中,例如要修改別人開發的代碼或調試出問題的代碼,需要對代碼流程一步步去跟蹤,找到出問題的地方進行修改。如果有一個方法可以擷取到某段代碼是被哪個方法調用,並能一直回溯到最開始調用的地方(包括調用的檔案,行數,參數等),這樣就能很方便的定位到出問題的地方。

php的debug_backtrace方法可以對代碼調用進行跟蹤,方便調試代碼。

debug_backtrace 方法說明

產生一條回溯跟蹤(backtrace)

array debug_backtrace ([ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT [, int $limit = 0 ]] )

參數

options

DEBUG_BACKTRACE_PROVIDE_OBJECT
是否填充 “object” 的索引。

DEBUG_BACKTRACE_IGNORE_ARGS
是否忽略 “args” 的索引,包括所有的 function/method 的參數,能夠節省記憶體開銷。

limit
這個參數能夠用於限制返回堆疊框架的數量,預設為(limit=0),返回所有堆疊框架。

傳回值

返回一個包含眾多關聯陣列的array,可能返回的元素:

名字         類型    說明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

<?phprequire 'order.php';// 擷取使用者訂單資料$order_id = 1000000;$oOrder = new Order;$order_info = $oOrder->get_order($order_id);?>

order.php

<?phprequire '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

<?phprequire '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 1000000

2.order.php
line 14
class User
function get_user
args int 1001

3.user.php
line 12
class Message
function get_message
args int 1001

本篇文章講解了通過php 實現debug_backtrace方法跟蹤代碼調用,更多相關內容請關注php中文網。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.