Howdy!
I am working on a pretty large project where I needed to dump a human readable form of whatever into the log files... and I thought var_export was too difficult to read. BigueNique at yahoo dot ca has a nice solution, although I needed to NOT modify whatever was being passed to dump.
So borrowing heavily from BigueNique's (just reworked his function) and someone's idea over in the object cloning page, I came up with the following function.
It makes a complete copy of whatever object you initially pass it, including all recursive definitions and outside objects references, then does the same thing as BigueNique's function. I also heavily reworked what it output, to suit my needs.
<?php
function var_log(&$varInput, $var_name='', $reference='', $method = '=', $sub = false) {
static $output ;
static $depth ;
if ( $sub == false ) {
$output = '' ;
$depth = 0 ;
$reference = $var_name ;
$var = serialize( $varInput ) ;
$var = unserialize( $var ) ;
} else {
++$depth ;
$var =& $varInput ;
}
$nl = "\n" ;
$block = 'a_big_recursion_protection_block';
$c = $depth ;
$indent = '' ;
while( $c -- > 0 ) {
$indent .= '| ' ;
}
if ( is_array($var) && isset($var[$block])) {
$real =& $var[ $block ] ;
$name =& $var[ 'name' ] ;
$type = gettype( $real ) ;
$output .= $indent.$var_name.' '.$method.'& '.($type=='array'?'Array':get_class($real)).' '.$name.$nl;
} else {
$var = Array( $block => $var, 'name' => $reference );
$theVar =& $var[ $block ] ;
$type = gettype( $theVar ) ;
switch( $type ) {
case 'array' :
$output .= $indent . $var_name . ' '.$method.' Array ('.$nl;
$keys=array_keys($theVar);
foreach($keys as $name) {
$value=&$theVar[$name];
var_log($value, $name, $reference.'["'.$name.'"]', '=', true);
}
$output .= $indent.')'.$nl;
break ;
case 'object' :
$output .= $indent.$var_name.' = '.get_class($theVar).' {'.$nl;
foreach($theVar as $name=>$value) {
var_log($value, $name, $reference.'->'.$name, '->', true);
}
$output .= $indent.'}'.$nl;
break ;
case 'string' :
$output .= $indent . $var_name . ' '.$method.' "'.$theVar.'"'.$nl;
break ;
default :
$output .= $indent . $var_name . ' '.$method.' ('.$type.') '.$theVar.$nl;
break ;
}
}
-- $depth ;
if( $sub == false )
return $output ;
}
?>
Hope it works well for you!