Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created October 3, 2014 16:16
Show Gist options
  • Save xeoncross/64077db877701816969f to your computer and use it in GitHub Desktop.
Save xeoncross/64077db877701816969f to your computer and use it in GitHub Desktop.
Simple PHP object dump for those large objects with huge blobs of text or deep nested structures
<?php
/**
* Simple object dump for those large objects with huge blobs of text or deep nested structures
*/
function objectDump($object, $size = 70, $max = 5, $level = 0)
{
if($level > $max) return 'array(...MAX DEPTH REACHED...)';
$tab = str_repeat("\t", $level);
$safe = function($string) use(&$size) {
$string = htmlspecialchars($string, ENT_QUOTES, 'utf-8');
$string = preg_replace("~\n\s*~", ' ', str_replace("\r", '', $string));
if(strlen($string) > $size) {
return substr($string, 0, $size) . '...';
}
return $string;
};
$temp = array_flip(array_keys( (array) $object));
foreach((array) $object as $key => $value) {
if( ! is_scalar($value) AND $value) {
$temp[$key] = objectDump($value, $size, $max, $level + 1);
continue;
}
$temp[$key] = ($value === NULL ? 'null' : $safe($value));
}
if($level === 0) {
print "<pre>\n" . print_r($temp, true) . "</pre>\n";
return;
}
return $temp;
}
@Eddcapone
Copy link

Eddcapone commented Sep 21, 2020

I get **Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/x/y/app/code/XXX/Shipping/Helper/Quote.php on line 232

#1 htmlspecialchars(array(), 3, 'utf-8') called at**

And $tab is not used in your function btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment