PHP uses the Debug_backtrace method to track code calls

Source: Internet
Author: User
During the development process, such as modifying code developed by someone else or debugging a problem, you need to track the code process step-by-step and find the place where the problem is to be modified. If there is a way to get to a piece of code is called by which method, and can always go back to the beginning of the call (including the number of files, rows, parameters, etc.), so that it can be easily located to the problem place.

The PHP debug_backtrace method can be used to track code calls and to facilitate debugging of code.

Debug_backtrace Method Description

Generate a backtracking trace (backtrace)

Array debug_backtrace ([int $options = debug_backtrace_provide_object [, int $limit = 0]])

Parameters

Options

Debug_backtrace_provide_object
Whether to populate the index of "object".

Debug_backtrace_ignore_args
Whether to ignore the "args" index, including all Function/method parameters, can save memory overhead.

Limit
This parameter can be used to limit the number of return stack frames, which defaults to (limit=0), returning all stack frames.


return value

Returns an array containing a number of associative arrays, possibly returning elements:

         The name Type    describes  the function string's current name, see: __function__.        Line integer The current row number. See also: __line__.        file string  the current filename. See also: __file__.  The name of the current class of class string. See __class__object      Object  , the current object.        type string  that is currently called. If it is a method, it returns "--". If it is a static method, it returns "::". If it is a function call, it returns NULL. Args        array   If in a function, this lists the parameters of the function. If it is in a contained file, the included file name is listed.

Instance

Obtain the user information of the order and the user message, the calling process is Index->order->user->message, and finally returns the collated information.

Suppose we debug when we find that the data of the message is wrong, you can use the Debug_backtrace method in message to see the process of the call and the parameters of the call, and check which step has the problem.

Using Debug_backtrace_ignore_args ignores ARGS (parameters of the method call)

index.php

<?phprequire ' order.php ';//get user order data $order_id = 1000000; $oOrder = new Order; $order _info = $oOrder->get_order ($ ORDER_ID);? >


order.php

<?phprequire ' user.php ';//Order Data Class order{    //Get Order data    function Get_order ($order _id) {        $user _id = 1001;        Get user profile        $oUser = new user;        $user _info = $oUser->get_user ($user _id);        Order data        $order _info = Array (            ' order_id ' = + $order _id,            ' order_name ' = ' my order ',            ' User_info ' =& Gt $user _info,        );        return $order _info;    }}? >


user.php

<?phprequire ' message.php ';//user profile class user{    //Get User data    function get_user ($user _id) {        //Get user info        $ 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//user Message Class message{    //Get user message    function get_message ($user _id) {        $message = array (            array ( ' id ' =>1, ' title ' = ' Message1 '),            array (' ID ' =>2, ' title ' = ' Message2 '),        );        Add Trace debug        $backtrace = Debug_backtrace ();        Var_dump ($backtrace);        return $message;    }}? >

run index.php, output

/message.php:15:array (size=3) 0 = = Array (size=7) ' file ' = = String '/user.php ' (length=9) ' line ' =&G T  int ' function ' = = String ' Get_message ' (length=11) ' class ' = = String ' message ' (length=7) ' object '          = = Object (Message) [3] ' type ' = ' + ' (length=2) ' args ' = Array (size=1)  0 = int 1001 1 = array (size=7) ' file ' = = String '/order.php ' (length=10) ' line ' = = int         + ' 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 ' funct Ion ' = = String ' Get_order ' (length=9) ' class ' = = String ' order ' (length=5) ' object ' = = Object (O Rder) [1] ' type '= = String ' (length=2) ' args ' = = Array (size=1) 0 = int 1000000 

You can see that the calling procedure is

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

This article explains how PHP uses the Debug_backtrace method to track code calls, and more about the PHP Chinese web.

Related recommendations:

How MySQL executes SQL at the terminal and writes the results to a file

PHP uses a token bucket algorithm based on Redis for traffic control

Redis master-Slave synchronization, read and write separation settings related operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.