Home  >  Article  >  Backend Development  >  Debugging function debug_backtrace in PHP

Debugging function debug_backtrace in PHP

不言
不言Original
2018-04-20 13:25:111529browse

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 can come and study 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, making it clear where it comes from.

Sometimes we want to know the call stack of this function or method, that is, how it is called level by level. We 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', '编辑成功!');
}

You can control the number of stack levels that need to be backtrace, of which debug_backtrace is the first The default parameter is 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 backtrace stacks. The default is all.

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

PHP official documentation: http:// php.net/manual/zh/function.debug-backtrace.php

Related recommendations:

phpstorm xdebug implements breakpoint debugging php

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

Statement:
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