PHP uses the debug_backtrace method to trace debug code calls

  • 2021-10-16 01:19:35
  • OfStack

This article example shows how PHP traces debug code calls using the debug_backtrace method. Share it for your reference, as follows:

In the development process, for example, to modify the code developed by others or debug the code with problems, it is necessary to track the code flow 1 step by step and find out the problems to modify. If there is a method that can get which method called a certain piece of code, and can go back to the place where the call started (including the file called, the number of lines, parameters, etc.), it is very convenient to locate the problem.

Using php debug_backtrace Method can trace code calls, which is convenient for debugging code. It can generate an associative array that collects the state information of the current application, and also provides the stack information of the current application and the method of storing the application in the stack.

debug_backtrace Method Description

Generate a backtracking trace (backtrace)

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

Parameter

options

DEBUG_BACKTRACE_PROVIDE_OBJECT
Whether to populate the index of "object".

DEBUG_BACKTRACE_IGNORE_ARGS
Whether to ignore the index of "args", including all function/method parameters, can save memory overhead.

limit

This parameter can be used to limit the number of returned stack frames, which defaults to (limit=0) and returns all stack frames.

Return value

Returns 1 array with many associative arrays. Possible elements returned:

名字 类型 说明
function string 当前的函数名,参见: __FUNCTION__。
line integer 当前的行号。参见: __LINE__。
file string 当前的文件名。参见: __FILE__。
class string 当前 class 的名称。参见 __CLASS__
object object 当前的 object。
type string 当前调用的类型。如果是1个方法,会返回 "->"。如果是1个静态方法,会返回 "::"。 如果是1个函数调用,则返回空。
args array 如果在1个函数里,这会列出函数的参数。 如果是在1个被包含的文件里,会列出包含的文件名。

Instances

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

Assuming that we find that the data of message is wrong during debugging, we can use it in message debug_backtrace Method, view the calling process and the parameters of the call, and check which step has problems.

Use DEBUG_BACKTRACE_IGNORE_ARGS args (parameter of method call) is ignored

index.php


<?php
require 'order.php';
//  Obtain user order data 
$order_id = 1000000;
$oOrder = new Order;
$order_info = $oOrder->get_order($order_id);
?>

order.php


<?php
require 'user.php';
//  Order information 
class Order{
 //  Obtain order information 
 function get_order($order_id){
  $user_id = 1001;
  //  Obtain user information 
  $oUser = new User;
  $user_info = $oUser->get_user($user_id);
  //  Order information 
  $order_info = array(
   'order_id' => $order_id,
   'order_name' => 'my order',
   'user_info' => $user_info,
  );
  return $order_info;
 }
}
?>

user.php


<?php
require 'message.php';
//  User profile 
class User{
 //  Obtain user information 
 function get_user($user_id){
  //  Getting User Messages 
  $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{
 //  Getting User Messages 
 function get_message($user_id){
  $message = array(
   array('id'=>1, 'title'=>'message1'),
   array('id'=>2, 'title'=>'message2'),
  );
  //  Join trace debugging 
  $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 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

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

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP Error and Exception Handling Methods", "Summary of php String (string) Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of PHP Operation and Operator Usage", "Introduction to PHP Basic Syntax", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this paper is helpful to everyone's PHP programming.


Related articles: