note 9570 deleted from function.var-dump by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 23:58:21 +0000 |
| Subject: | note 9570 deleted from function.var-dump by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22399@lists.php.net to get a copy of this message | ||
My only complaint with var_dump is that it dumps strings as is which means I can't dump them to
a web page if they contain HTML. So I wrote a replacement below:
function mydump1 ($thing, $maxdepth, $depth) {
$fmt = sprintf ("%%%ds", 4*$depth);
$pfx = sprintf ($fmt, "");
$type = gettype($thing);
if ($type == 'array') {
$n = sizeof($thing);
echo "$pfx<B>array($n) =></B><BR />";
foreach (array_keys($thing) as $key) {
echo " $pfx<B>[$key] =></B>\n";
mydump1 ($thing[$key], $maxdepth, $depth+1);
}
} else if ($type == 'string') {
$n = strlen($thing);
echo "$pfx<B>string($n) =></B>\n";
echo "$pfx\"" . htmlentities($thing) . "\"\n";
} else if ($type == 'object') {
$name = get_class($thing);
echo "$pfx<B>object($name) =></B>\n";
$methodArray = get_class_methods($name);
foreach (array_keys($methodArray) as $m) {
echo " $pfx<B>method($m) =></B> $methodArray[$m]\n";
}
$classVars = get_class_vars($name);
foreach (array_keys($classVars) as $v) {
echo " $pfx<B>default => $v =></B>\n";
mydump1($classVars[$v], $maxdepth, $depth+2);
}
$objectVars = get_object_vars($thing);
foreach (array_keys($objectVars) as $v) {
echo " $pfx<B>$v =></B>\n";
mydump1($objectVars[$v], $maxdepth, $depth+2);
}
} else if ($type == 'boolean') {
if ($type) {
echo "$pfx<B>boolean(true)</B><BR />";
} else {
echo "$pfx<B>boolean(false)</B><BR />";
}
} else {
echo "$pfx<B>$type($thing)</B><BR />";
}
}
function mydump ($thing, $maxdepth=-1) {
echo "<PRE>\n";
mydump1 ($thing, $maxdepth, 0);
echo "</PRE>\n";
}