Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Last active June 29, 2020 21:35
Show Gist options
  • Save UVLabs/692e542d3b53e079d36bc53b4ea20a4b to your computer and use it in GitHub Desktop.
Save UVLabs/692e542d3b53e079d36bc53b4ea20a4b to your computer and use it in GitHub Desktop.
Outputs an easy to read call trace. Credit: https://www.php.net/manual/en/function.debug-backtrace.php#112238
<?php
// Outputs an easy to read call trace
// Credit: https://www.php.net/manual/en/function.debug-backtrace.php#112238
function generateCallTrace()
{
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++)
{
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
}
return "\t" . implode("\n\t", $result);
}
// call function where needed to output call trace
/**
Example output:
1) /var/www/test/test.php(15): SomeClass->__construct()
2) /var/www/test/SomeClass.class.php(36): SomeClass->callSomething()
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment