note 76072 added to function.var-dump

From: Date: Thu, 28 Jun 2007 20:08:49 +0000
Subject: note 76072 added to function.var-dump
Groups: php.notes 
Request: Send a blank email to php-notes+get-128063@lists.php.net to get a copy of this message
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 ; } // constants $nl = "\n" ; $block = 'a_big_recursion_protection_block'; $c = $depth ; $indent = '' ; while( $c -- > 0 ) { $indent .= '| ' ; } // if this has been parsed before 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; // havent parsed this before } else { // insert recursion blocker $var = Array( $block => $var, 'name' => $reference ); $theVar =& $var[ $block ] ; // print it out $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 ; } // $var=$var[$block]; } -- $depth ; if( $sub == false ) return $output ; } // var_log( $var, '$name' ) ; ?> Hope it works well for you! ---- Server IP: 69.147.83.197 Probable Submitter: 99.247.248.117 ---- Manual Page -- http://www.php.net/manual/en/function.var-dump.php Edit -- https://master.php.net/note/edit/76072 Del: integrated -- https://master.php.net/note/delete/76072/integrated Del: useless -- https://master.php.net/note/delete/76072/useless Del: bad code -- https://master.php.net/note/delete/76072/bad+code Del: spam -- https://master.php.net/note/delete/76072/spam Del: non-english -- https://master.php.net/note/delete/76072/non-english Del: in docs -- https://master.php.net/note/delete/76072/in+docs Del: other reasons-- https://master.php.net/note/delete/76072 Reject -- https://master.php.net/note/reject/76072 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128063) next »