Skip to content

Instantly share code, notes, and snippets.

@jgusta
Created November 25, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgusta/67fb5ce905a7ccc0e8e8f86eb9352cae to your computer and use it in GitHub Desktop.
Save jgusta/67fb5ce905a7ccc0e8e8f86eb9352cae to your computer and use it in GitHub Desktop.
A function that returns a big string version of var_dump or print_r except it descends into private props and shows things in as compact manner as possible.
<?php
if (!function_exists('dump_walk')) {
/**
* This function is like print_r($node, true) but cleaner
* @param $node
* @param string $indent_string
* @param int $depth
* @param null $parentType
* @return string
*/
function dump_walk($node, $indent_string = ' ', $depth = 0, $parentType = null, $objects_shown = []) {
try {
$append = '';
if (is_array($node)) {
$nodeType = 'array';
}
else if (is_object($node)) {
$nodeType = 'object';
}
else {
$nodeType = 'leaf';
}
if (null === $parentType) {
if ($nodeType !== 'object' && $nodeType !== 'array') {
if (gettype($node) === 'string') {
$append .= "Root ($nodeType) '$node'";
}
else {
$append .= "Root ($nodeType) $node";
}
}
else {
if ($nodeType === 'array') {
$append .= "Root array(" . count($node) . ")\n";
}
else if ($nodeType === 'object') {
$append .= 'Root object ' . get_class($node) . "\n";
}
$depth += 1;
}
}
if ($nodeType === 'array') {
foreach ($node as $k => $child) {
$branch = false;
for ($i = 0; $i < $depth; $i++) {
$append .= $indent_string;
}
if (is_array($child)) {
$branch = true;
$childNodeType = "array (" . count($child) . ")";
}
else if (is_object($child)) {
if ($child instanceof \DateTime) {
$child->setTimezone(new \DateTimeZone('America/Los_Angeles'));
$childNodeType = 'object ' . get_class($child);
$child = $child->format("Y-m-d g:i:s a e");
}
else if ($child instanceof mysqli) {
$childNodeType = 'object mysqli' . get_class($child);
}
else if ($child instanceof mysqli_result) {
$childNodeType = 'object mysqli_result';
}
else {
$branch = true;
$childNodeType = 'object ' . get_class($child);
}
}
else {
$childNodeType = '(' . gettype($child) . ')';
}
if ($branch) {
$append .= "['$k'] => $childNodeType\n" . dump_walk($child, $indent_string, $depth + 1, $nodeType, $objects_shown);
}
else {
if (gettype($child) === 'string') {
$append .= "['$k'] => $childNodeType '$child'\n";
}
elseif (gettype($child) === 'boolean') {
$append .= "['$k'] => $childNodeType ".($child?"true":"false")."\n";
}
elseif (is_null($child)) {
$append .= "['$k'] => $childNodeType ".'NULL'."\n";
}
elseif( is_object($child) ) {
// non-branching object, don't try and cast to string
$append .= "['$k'] => $childNodeType\n";
}
else {
$append .= "['$k'] => $childNodeType $child\n";
}
}
}
}
else if ($nodeType === 'object') {
$reflect = new ReflectionObject($node);
$props = $reflect->getProperties();
if (!in_array(spl_object_hash($node), $objects_shown)) {
$objects_shown[] = spl_object_hash($node);
}
else {
for ($i = 0; $i < $depth; $i++) {
$append .= $indent_string;
}
return "$append"."(...)\n";
}
foreach ($props as $prop) {
$is_static = $prop->isStatic();
$visibility = $prop->isPrivate() ? 'private:' : ''
. $prop->isPublic() ? 'public:' : ''
. $prop->isProtected() ? 'protected:' : '';
$prop->setAccessible(true);
for ($i = 0; $i < $depth; $i++) {
$append .= $indent_string;
}
$branch = false;
$child = $prop->getValue($node);
if (is_array($child)) {
$branch = true;
$childNodeType = "array (" . count($child) . ")";
}
else if (is_object($child)) {
if ($child instanceof \DateTime) {
$child->setTimezone(new \DateTimeZone('America/Los_Angeles'));
$childNodeType = 'object ' . get_class($child);
$child = $child->format("Y-m-d g:i:s a e");
}
else if ($child instanceof mysqli) {
$childNodeType = 'object mysqli' . get_class($child);
}
else if ($child instanceof mysqli_result) {
$childNodeType = 'object mysqli_result';
}
else {
$branch = true;
$childNodeType = 'object ' . get_class($child);
}
}
else {
$childNodeType = '(' . gettype($child) . ')';
}
if ($branch) {
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType\n" . dump_walk($prop->getValue($node), $indent_string, $depth + 1, $nodeType, $objects_shown);
}
else {
if (gettype($child) === 'string') {
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType '$child'\n";
}
elseif (gettype($child) === 'boolean') {
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType ".($child?"true":"false")."\n";
}
elseif (is_null($child)) {
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType ".'NULL'."\n";
}
elseif( is_object($child) ) {
// non-branching object, don't try and cast to string
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType\n";
}
else {
$append .= "->{$prop->getName()}:{$visibility}" . ($is_static ? ':static' : '') . " = $childNodeType $child\n";
}
}
}
}
return $append;
}
catch (\Exception $e) {
return 'dump_walk() error: ' . $e->getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment