var_dump/print_r
| From: | mailling at bigfoot dot com | Date: | Wed, 01 Aug 2001 18:50:35 +0000 |
| Subject: | var_dump/print_r | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-1210@lists.php.net to get a copy of this message | ||
Sometime agoi, I realized a method equivalent to print_r, but with a much better support to avoid circular reference, better color, and so on
I think it could be interesting to add a directory Error or Debug, and put this method there:
Rigth now, it is a method, but it would be pretty easy to call it this way: VAR_DUMP::var_dumpr(...);
What do you think?
/**
* Equivalent of var_dump, but much nicer
* Dump to the screen the content of a variable, object or array
* If we are working with an object, we will see also the list of the methods
* to avoid cycle pointer reference, it is possible to specify a maximum depth
*
* @param mixed $a Variable to check in
* @param optional string $pre Used to ident correctly the levels,
* what to put before* @param optional integer $limitdepth What is the limit depth * @param optional boolean $firstcall Must be set to true if it is the first
* time you call this method* @param optional string $before What to put before beginning the process * @param optional string $before What to put after finishing the process * * @author Stephane Huther <shuther@bigfoot.com> */ /** @constant String VAR_DUMPR_LIMIT_OVER limit over*/ define ('VAR_DUMPR_LIMIT_OVER','(LIMIT DEPTH OVER)'); /** @constant String VAR_DUMPR_NO_ELEMENT no more element*/ define ('VAR_DUMPR_NO_ELEMENT','NO ELEMENT'); function var_dumpr(&$a,$pre='', $limitdepth='5', $firstcall=true, $before='<font color="#0033FF" face="Verdana" size="1">', $after='</font><br/>') { if (0==$limitdepth) { printf('%s %s %s<br/>',$pre, $a, VAR_DUMPR_LIMIT_OVER); return; } if ($firstcall) print $before; $c=gettype($a); $toshow=false; switch ($c) { case 'array': printf('%sArray<br/>', $pre); $pre=$pre . ' '; if (count($a)=='0') printf('%s %s',$pre, VAR_DUMPR_NO_ELEMENT); else foreach ($a as $key=>$val) { printf('%sKey: %s(%s)', $pre, $key, gettype($key)); var_dumpr($val, $pre, $limitdepth-1, false); } break; case 'boolean': $a=$a ? 'true' : 'false'; $toshow=true; break; case 'object': printf('%sObject: %s<br/>', $pre, get_class($a)); $pre=$pre . ' '; $d=get_object_vars($a); if (count($d)=='0') printf('%s %s',$pre, VAR_DUMPR_NO_ELEMENT); else foreach ($d as $key=>$val) { printf('%sVariable: %s', $pre, $key); var_dumpr($val, $pre, $limitdepth-1, false); } $e=get_class_methods(get_class($a)); foreach ($e as $val) { printf('<br/>%sMethod: %s', $pre, $val); } break; default: $toshow=true; } if ($toshow) printf('%s %s (%s)', $pre, $a, $c); if ($firstcall) print $after; else printf('<br/>'); }