<?php
/**
* Class to display debugging information of any PHP script
*
* @author Rochak Chauhan
*
*/
class PhpDebugger {
private $shouldIncludeCallToPhpDebugger = 1;
/**
* Fucntion to return an array of debugging information
*
* @param $shouldIncludeCallToPhpDebugger boolean
*
* @return array
*/
public function displayDebuggingInformation($shouldIncludeCallToPhpDebugger = false) {
$debuggingInformationArray = Array();
if($shouldIncludeCallToPhpDebugger === true) {
$this->shouldIncludeCallToPhpDebugger = 0;
}
$debugInfoArray = debug_backtrace();
for($i=$this->shouldIncludeCallToPhpDebugger; $i<count($debugInfoArray); $i++) {
if(count($debugInfoArray[$i]['args']) == 0) {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Current File: </b></font>".$debugInfoArray[$i]['file'];
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Line no: </b></font>".$debugInfoArray[$i]['line'];
if(isset($debugInfoArray[$i]['class'])) {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Action performed: </b></font>".$debugInfoArray[$i]['class'].$debugInfoArray[$i]['type'].$debugInfoArray[0]['function'];
}
if(isset($debugInfoArray[$i]['function'])) {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Function called : </b></font>".$debugInfoArray[$i]['function']."()";
}
}
else {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Current File: </b></font>".$debugInfoArray[$i]['file'];
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Line no: ".$debugInfoArray[$i]['line'];
if(isset($debugInfoArray[$i]['class'])) {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Action performed: </b></font>".$debugInfoArray[$i]['class'].$debugInfoArray[$i]['type'].$debugInfoArray[0]['function'];
}
if(isset($debugInfoArray[$i]['function'])) {
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Function called : </b></font>".$debugInfoArray[$i]['function']."()";
}
$debuggingInformationArray[] = "<font face='verdana' size='2'><b>Arguments: </b></font>".$this->displayArray($debugInfoArray[$i]['args']);
}
}
$this->displayArrayAsTable($debuggingInformationArray);
}
/**
* Function to display an array as a string( new line seperated)
*
*
* @access private
* @param $array - input array
*
* @return string
*/
private function displayArray($array) {
$returnString = '';
foreach($array as $value) {
$returnString .= $value."</ BR>";
}
return $returnString;
}
/**
* Function to display an array as table
*
* @access private
*
* @param $array - input array
*
* @return string - Html Code to display array as a table
*/
private function displayArrayAsTable($array) {
$cnt = 0;
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
foreach($array as $value) {
if($cnt%2 ==0) {
$color = "lightblue";
}
else {
$color = "pink";
}
$cnt++;
if( substr_count($value,'Current File:') ) {
echo "<tr><td><BR><BR></td></tr>";
}
echo "<tr style='background-color:$color'><td>$value</td></tr>";
}
echo "</table>";
}
}
/*
USAGE
require_once("PhpDebugger.inc.php");
$phpDebugger = new PhpDebugger;
// true indicates that the call to PhpDebugger will also be traced
$phpDebugger->displayDebuggingInformation(true);
*/
?>