Skip to content

Instantly share code, notes, and snippets.

@terpfear
Last active September 21, 2015 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terpfear/4adab63546659d1f3cab to your computer and use it in GitHub Desktop.
Save terpfear/4adab63546659d1f3cab to your computer and use it in GitHub Desktop.
php backtrace bug
<?php
function brokenTrace($arg1, $arg2){
backtraceWrapper();
echo exceptionTraceWrapper();
echo "\n";
echo "unsetting arg1\n";
unset($arg1);
backtraceWrapper();
echo exceptionTraceWrapper();
echo "\n";
echo "unsetting arg2\n";
unset($arg2);
backtraceWrapper();
echo exceptionTraceWrapper();
}
brokenTrace("string", "anything else");
echo "\n";
function backtraceWrapper(){
$bt = debug_backtrace();
var_dump($bt[1]['args']);
}
function exceptionTraceWrapper()
{
$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) . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment