Detailed explanation of the use of debug_backtrace debugging function in php

黄舟
Release: 2023-03-15 22:00:01
Original
1309 people have browsed it

debug_backtrace() is a very low-key function, and few people have paid attention to it. This article mainly introduces you to the use of the debug function debug_backtrace in PHP. The article introduces it in detail through example code, which is very helpful to everyone. Studying or working has certain reference learning value. Friends who are interested should follow the editor to learn together.

Preface

If we want to know who called a certain method? debug_backtrace can solve it. debug_backtrace() can print out the calling process of a page, where it comes from and where it goes. Sometimes we want to know the call stack of this function or method, that is, how it is called level by level. If you arrive, you can use PHP's debug_backtrace function to print it, like this:

Sample code

public function update(Request $request, $id)
{
 dd(debug_backtrace());
 $getGameID = function ($request) {
 if (!$request->game_id) {
  return 1000 + intval($request->id);
 }
 return $request->game_id;
 };
 
 $previews = $this->getGamePreviews($request->game_preview);
 
 $request->merge([
 'game_preview' => json_encode($previews),
 'game_id' => $getGameID($request)
 ]);
 EgretGame::where('id', $id)->update($request->except(['_token', '_method']));
 return redirect()->route('egretgame.index')->with('success', '编辑成功!');
}
Copy after login

You can control the number of stack levels that need to be traced back. The first parameter of debug_backtrace defaults to a constant

DEBUG_BACKTRACE_PROVIDE_OBJECT

, which indicates that the information of this object is displayed. The second parameter is used to control the number of stacks to backtrace. The default is all.

The effect is as shown in the figure, and the relationship between the calling levels is clear at a glance:

Summary

The above is the detailed content of Detailed explanation of the use of debug_backtrace debugging function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!