PHP debug_backtrace - Syntax
debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array
debug_backtrace() returns an array of stack frames. Each frame describes one call and may include a function name, file, line, arguments, class, or object.
debug_backtrace()
debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array
| $options | Use DEBUG_BACKTRACE_IGNORE_ARGS to omit argument values. The default is DEBUG_BACKTRACE_PROVIDE_OBJECT. |
|---|---|
| $limit | Maximum frame count. Zero includes the whole stack. |
DEBUG_BACKTRACE_PROVIDE_OBJECT includes the object in applicable frames returned by debug_backtrace().
DEBUG_BACKTRACE_IGNORE_ARGS omits argument values from stack frames.
A teacher wants to see how the welcome page reached its greeting helper. Inspect the stack at greetings() and follow the calling frames outward.
<?php
class HelloWorld {
public function greetings() {
echo("Hello World \n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_PROVIDE_OBJECT and limit 0
$trace_array = debug_backtrace();
print_r($trace_array);
}
}
$obj = new HelloWorld();
$obj->greetings();
Hello World
Array
(
[0] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 15
[function] => greetings
[class] => HelloWorld
[object] => HelloWorld Object
(
)
[type] => ->
[args] => Array
(
)
)
)A nested message helper produces unexpected output on a class page. Start inside printMessage() and follow the callers that reached that method.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_PROVIDE_OBJECT and limit 0
$trace_array = debug_backtrace();
print_r($trace_array);
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John
Array
(
[0] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 8
[function] => printMessage
[class] => HelloWorld
[object] => HelloWorld Object
(
)
[type] => ->
[args] => Array
(
[0] => Hello John
)
)
[1] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 22
[function] => greetings
[class] => HelloWorld
[object] => HelloWorld Object
(
)
[type] => ->
[args] => Array
(
[0] => Hello John
)
)
)A support engineer needs to identify the calling functions for a failed classroom request. Omit argument values when the frame names alone answer the question.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_IGNORE_ARGS and limit 0
$trace_array = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
print_r($trace_array);
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John
Array
(
[0] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 8
[function] => printMessage
[class] => HelloWorld
[type] => ->
)
[1] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 22
[function] => greetings
[class] => HelloWorld
[type] => ->
)
)A busy help desk needs a compact trace for a particular request. Limit it to one frame and omit arguments to show just the immediate call site.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_IGNORE_ARGS and limit 1
$trace_array = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
print_r($trace_array);
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John
Array
(
[0] => Array
(
[file] => ~\Documents\debug-backtrace.php
[line] => 8
[function] => printMessage
[class] => HelloWorld
[type] => ->
)
)Reference: PHP manual — debug_backtrace()