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 $options $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:

Name Type descriptionfunction    stringThe current function name, see:__function__. LineintegerThe current line number. See:__line__. file        stringThe current file name. See:__file__. class       stringCurrentclass's name. See__class__Object      ObjectThe currentObject. TypestringThe type of the current call. If it is a method, it returns "--". If it is a static method, it returns "::". If it is a function call, it returns NULL. ArgsArrayIf 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

<? PHP require ' order.php '; // Get user order information $order _id = 1000000; $oOrder New Order; $order _info $oOrder->get_order ($order _id);? >

order.php

<?PHPrequire' User.php ';//Order Detailsclassorder{//Get Order Details    functionGet_order ($order _id){        $user _id= 1001; //Get user Profile        $oUser=NewUser; $user _info=$oUser->get_user ($user _id); //Order Details        $order _info=Array(            ' order_id ' =$order _id, ' order_name ' = ' my order ', ' user_info '$user _info,        ); return $order _info; }}?>

user.php

<?PHPrequire' Message.php ';//User Profileclassuser{//Get user Profile    functionGet_user ($user _id){        //Get user Information        $oMessage=NewMessage; $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 Informationclassmessage{//Get user Information    functionGet_message ($user _id){        $message=Array(            Array(' id ' =>1, ' title ' = ' Message1 '),Array(' id ' =>2, ' title ' = ' Message2 '),        ); //Join 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 ' = int ' function ' =string' Get_message ' (length=11)      ' Class ' =string' Message ' (length=7)      ' object ' = =Object(Message) [3]      ' type ' = =string' and ' (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' and ' (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' and ' (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

PHP uses the Debug_backtrace method to track code calls

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.