Last active
December 12, 2015 08:18
-
-
Save kbanman/4742949 to your computer and use it in GitHub Desktop.
Rich alternative to var_dump()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Pretty dump and die | |
*/ | |
function dd($var, $depth=0) | |
{ | |
static $test; | |
if ($depth > 5) { | |
return 'TOO MANY TURTLES!'; | |
} | |
//$br = '<br>'; | |
$br = ''; | |
//$tab = " "; | |
$tab = ''; | |
$tab0 =str_repeat($tab, $depth); | |
$tab1 = str_repeat($tab, $depth+1); | |
$t = "\t"; | |
$t0 = str_repeat($t, $depth); | |
$t1 = str_repeat($t, $depth+1); | |
$nl0 = $br . $tab0 . "\n" . $t0; | |
$nl1 = $br . $tab1 . "\n" . $t1; | |
$output = ''; | |
if ( ! $depth) { | |
if (isset($test)) { | |
die('wtdf'); | |
} | |
$test = true; | |
ob_end_clean(); | |
$trace = debug_backtrace(); | |
$output .= ' | |
<html><head> | |
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> | |
<style> | |
body { | |
font-family: fixed-width; | |
} | |
.collapse:not(.depth0) .title { | |
cursor: pointer; | |
background-color: #777; | |
color: #fff; | |
border-radius: 2px; | |
padding: 1px 3px; | |
} | |
.collapse:not(.depth0) .content{ | |
display: none; | |
} | |
</style> | |
</head><body> | |
<p>'. basename($trace[0]['file']) .' line '. $trace[0]['line'].'</p> | |
<pre>'; | |
} | |
// Objects | |
if (is_object($var)) { | |
$classname = get_class($var); | |
$class = new ReflectionClass($classname); | |
$output .= '<span class="collapse object depth'.$depth.'"><span class="title">'.$classname.'</span> <span class="content"> {'; | |
foreach ($class->getProperties() as $prop) { | |
$output .= $nl1; | |
if ($prop->isPrivate()) $output .= 'private '; | |
elseif ($prop->isProtected()) $output .= 'protected '; | |
elseif ($prop->isPublic()) { | |
$output .= 'public '; | |
} | |
$prop->setAccessible(true); | |
try { | |
$value = $prop->getValue($var); | |
} catch (Exception $e) { | |
$value = 'private/protected'; | |
} | |
$output .= '$' . $prop->name . ' = ' . dd($value, $depth+1); | |
} | |
$output .= "\n$br"; | |
foreach ($class->getMethods() as $method) { | |
$output .= $nl1; | |
if ($method->isPrivate()) $output .= 'private '; | |
elseif ($method->isProtected()) $output .= 'protected '; | |
elseif ($method->isPublic()) $output .= 'public '; | |
$params = []; | |
foreach ($method->getParameters() as $param) | |
{ | |
array_push($params, $param->getName()); | |
} | |
if (count($params) || $method->isPrivate()) { | |
$output .= $method->name.'('.implode(', ', $params).')'; | |
} else { | |
$method->setAccessible(true); | |
try { | |
$value = dd($method->invoke($var), $depth+1); | |
} catch (Exception $e) { | |
$value = 'Exception: '.$e->getMessage(); | |
} | |
$output .= $method->name.'() -> '.$value; | |
} | |
} | |
$output .= "$nl0}</span></span>"; | |
} | |
// Arrays | |
elseif (is_array($var)) { | |
$output .= '<span class="collapse array depth'.$depth.'"><span class="title">Array</span> <span class="content">('; | |
foreach ($var as $key => $val) { | |
$output .= $nl1; | |
if (is_object($val) || is_array($val)) { | |
$val = dd($val, $depth+1); | |
} | |
$output .= "'$key' => $val"; | |
} | |
$output .= "$nl0)</span></span>"; | |
} | |
// Simple vars | |
else { | |
$output .= htmlentities(var_export($var, true)); | |
} | |
if ( ! $depth) { | |
echo $output; | |
echo ' | |
</pre> | |
<script> | |
(function() { | |
$(".collapse .title").click(function(e){ | |
var $content = $(this).siblings(".content"); | |
if ($content.is(":visible")) { | |
$content.hide(); | |
} else { | |
$content.show(); | |
} | |
}) | |
}()); | |
</script> | |
</body></html>'; | |
die(); | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment