Location: PHPKode > scripts > varDebugger > vardebugger/debug.php
<?php
/*

NOTE: SOME OF THE LINES OF CODE WAS NOT WRITTEN BY ME, I AM NOT SURE WHERE I FOUND IT AT ANYMORE, BUT I REALLY LIKED IT SO I THOUGHT OTHERS WOULD ALSO


	.:: Example usage {
			$debug = new debugger("/home/sites/www.harrisburgarea.com/web/ccs/test/debug.txt"); // to file
			$debug->debug($_SERVER);

                        $debug = new debugger(); // to screen
			$debug->debug($_SERVER);

		}
*/

class debugger {
	var $output_to_file = false;
	var $output = "SCREEN";
	function debugger($output="SCREEN"){
		$this->output = $output;
		if(strtoupper($output) != "SCREEN") {
			$this->_output_to_file = true;
			$this->stamp = date("Y-m-d H:i",time());
			if(!$this->append($this->output,"DEBUG STARTED:".$this->stamp)){
				die("unable to create debug file: ".$output);
			}
		}
	}
	function append($file, $data, $glue = "\n") {
		$rtrn = false;
		if (@file_exists($file)) {
			if (@filesize($file) == 0 )  { $glue = ""; }
		} else { $glue = ""; }
		if ($handle = @fopen($file, 'a')) {
			if (@flock($handle,LOCK_EX)) {
   				if (@fwrite($handle, $glue.$data)) {
					$rtrn = true;
    			}
				@fflush($handle);
				@flock($handle,LOCK_UN);
			}
    		@fclose($handle);
		} 
		return $rtrn;
	}
	function type_it($rtrn) {
		if (is_bool($rtrn)) {
			if ($rtrn) { $rtrn = "true"; }
			else { $rtrn = "false"; }
			$rtrn = "boolean ($rtrn)";
		} elseif (is_null($rtrn)) { $rtrn = "null";	} 
		elseif (is_string($rtrn)) { $rtrn ="string (".strlen($rtrn).") \"".$rtrn."\""; }
		elseif (is_float($rtrn)) { $rtrn = "float ($rtrn)";	}
		elseif (is_int($rtrn)) { $rtrn = "integer ($rtrn)";	}
		else { $rtrn = "\"$rtrn\"";	}
		return $rtrn;
	} 
	function object_info($obj,$sp="") {
    	$out[] = $sp."Class: ".get_class($obj);
		$out[] = $sp." properties:";
    	foreach(get_object_vars($obj) as $var=>$val) {
    	    if (is_array($val)) { $out[] = $sp."  $var (array)\n".$this->p_arr($val,$sp."\t"); }
    	    elseif (is_object($val)) { $out[] =$sp."  $var: (object)".$this->object_info($val,$sp."\t"); }
			else { $out[] = $sp."  $var = ".$this->type_it($val); }
		}
		$out[] = $sp." methods:";
    	foreach(get_class_methods($obj) as $method) { $out[] = $sp."  ".$method."()"; }
    	return "<pre>".implode("\n", $out)."</pre>";
	}
	function p_arr($arr,$sp="") {
    	foreach($arr as $k=>$v){
			if (is_object($v)){ $out[] = $sp."[$k] => (object) ".$this->object_info($v,$sp."\t"); }
			elseif ( is_array($v)) { $out[] = $sp."[$k] => (array) \n".$this->p_arr($v,$sp."\t"); }
			else { $out[] = $sp."[$k] => ".$this->type_it($v);	}
		}
		if (is_array($out)) { $rtrn = implode("\n", $out); }
		else { $rtrn = $out; }
    	return $rtrn;
	}
	function print_it($text){
  		return "<pre>".$text."</pre>";
	}	
	function debug($info){
		if(is_array($info)) {
			$rtrn = "<br>DEBUG: Dumping Array";
	      	$rtrn .= $this->print_it($this->p_arr($info));
	    } elseif (is_object($info)) {
			$rtrn = "<br>DEBUG: Dumping Object ".get_class($info);
			$rtrn .= $this->object_info($info);
		} else {
			$rtrn = "<br>DEBUG: ".$this->print_it($this->type_it($info));
		}
		if ($this->_output_to_file) {
			$this->append($this->output,"DUMP: ".$this->stamp."\n".$rtrn."\nEND DUMP");
		} else {
			echo $rtrn;
		}			
	}
}
?>
Return current item: varDebugger