Skip to content

Instantly share code, notes, and snippets.

@GuilhermeRossato
Last active January 29, 2018 01:32
Show Gist options
  • Save GuilhermeRossato/682d98d8da2f43cd183db9837267e85e to your computer and use it in GitHub Desktop.
Save GuilhermeRossato/682d98d8da2f43cd183db9837267e85e to your computer and use it in GitHub Desktop.
PHP retrieve better information from debug_backtrace without exausting memory
<?php
/*
* Output example at the bottom of this gist
*/
function get_formatted_debug_backtrace() {
$content = debug_backtrace();
$ret = [];
foreach($file_paths AS $file_path) {
$var = $file_path;
$ret[] = "File: ".$var['file'].": ".$var['line']."\n";
$ret[] = $var['class'].$var['type'].$var['function']."(";
$retInit = false;
if (isset($file_path['args'])) {
$var = $file_path['args'];
foreach($var AS $key=>$value) {
if (!$retInit) { $retInit = true; $ret[] = "\n"; }
$ret[] = "\t[$key] => ";
$type = gettype($value);
if ($type == "string" || $type == "number") {
$ret[] = var_export($value, true).",\n";
} else if ($type == "array") {
$sub = [];
foreach ($value as $subkey=>$subvalue) {
$subtype = gettype($subvalue);
if ($subtype === "string") {
$sub[] = var_export($subvalue, true);
} else {
$sub[] = "(".gettype($subvalue).")";
}
}
$ret[] = "[".implode(", ", $sub)."],\n";
} else {
$ret[] = "($type),\n";
}
}
}
$ret[] = ")\n";
}
$formattedResult = implode("", $ret);
return $formattedResult;
}
/* Output example below */
/*
File: /home/facasc/public_html/app/code/core/Mage/CatalogSearch/Model/Layer.php: 106
get_formatted_debug_backtrace()
File: /home/facasc/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php: 254
Mage_Core_Controller_Varien_Action->dispatch(
[0] => 'index',
)
File: /home/facasc/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php: 172
Mage_Core_Controller_Varien_Router_Standard->match(
[0] => (object),
)
File: /home/facasc/public_html/app/code/core/Mage/Core/Model/App.php: 365
Mage_Core_Controller_Varien_Front->dispatch()
File: /home/facasc/public_html/app/Mage.php: 684
Mage_Core_Model_App->run(
[0] => ['', 'store', (array)],
)
File: /home/facasc/public_html/index.php: 83
Mage::run(
[0] => '',
[1] => 'store',
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment