Created
July 10, 2018 14:24
-
-
Save alfredleo/f04903f4ceb15df0dbc7eff7d2d7450c to your computer and use it in GitHub Desktop.
Debug function for any php project. Works fine with ajax debug, remote user debug. tail -f for log view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Include this file in the starting point of application | |
* change error log location | |
* Usage: dumppp($test); | |
*/ | |
// normal dump won't work correctly on cyclic objects | |
include_once('TVarDumper.php'); | |
// set error log file, if not set properly, change this with your own location | |
ini_set("error_log", "c:\wamp64\logs\php_error.log"); | |
// switch to show or hide errors, by default you show all errors to debug | |
$show_errors = true; | |
if ($show_errors) { | |
ini_set("log_errors", 1); | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
} else { | |
ini_set("log_errors", 0); | |
error_reporting(E_ERROR); | |
ini_set("display_errors", 0); | |
} | |
// this is the testing function | |
function dumppp($any_mixed) | |
{ | |
error_log(TVarDumper::dump($any_mixed)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* TVarDumper class file | |
* | |
* @author Qiang Xue <[email protected]> | |
* @link http://www.pradosoft.com/ | |
* @copyright Copyright © 2005-2013 PradoSoft | |
* @license http://www.pradosoft.com/license/ | |
* @version $Id$ | |
* @package System.Util | |
*/ | |
/** | |
* TVarDumper class. | |
* | |
* TVarDumper is intended to replace the buggy PHP function var_dump and print_r. | |
* It can correctly identify the recursively referenced objects in a complex | |
* object structure. It also has a recursive depth control to avoid indefinite | |
* recursive display of some peculiar variables. | |
* | |
* TVarDumper can be used as follows, | |
* <code> | |
* echo TVarDumper::dump($var); | |
* </code> | |
* | |
* @author Qiang Xue <[email protected]> | |
* @version $Id$ | |
* @package System.Util | |
* @since 3.0 | |
*/ | |
class TVarDumper | |
{ | |
private static $_objects; | |
private static $_output; | |
private static $_depth; | |
/** | |
* Converts a variable into a string representation. | |
* This method achieves the similar functionality as var_dump and print_r | |
* but is more robust when handling complex objects such as PRADO controls. | |
* @param mixed variable to be dumped | |
* @param integer maximum depth that the dumper should go into the variable. Defaults to 10. | |
* @return string the string representation of the variable | |
*/ | |
public static function dump($var,$depth=10,$highlight=false) | |
{ | |
self::$_output=''; | |
self::$_objects=array(); | |
self::$_depth=$depth; | |
self::dumpInternal($var,0); | |
if($highlight) | |
{ | |
$result=highlight_string("<?php\n".self::$_output,true); | |
return preg_replace('/<\\?php<br \\/>/','',$result,1); | |
} | |
else | |
return self::$_output; | |
} | |
private static function dumpInternal($var,$level) | |
{ | |
switch(gettype($var)) | |
{ | |
case 'boolean': | |
self::$_output.=$var?'true':'false'; | |
break; | |
case 'integer': | |
self::$_output.="$var"; | |
break; | |
case 'double': | |
self::$_output.="$var"; | |
break; | |
case 'string': | |
self::$_output.="'$var'"; | |
break; | |
case 'resource': | |
self::$_output.='{resource}'; | |
break; | |
case 'NULL': | |
self::$_output.="null"; | |
break; | |
case 'unknown type': | |
self::$_output.='{unknown}'; | |
break; | |
case 'array': | |
if(self::$_depth<=$level) | |
self::$_output.='array(...)'; | |
else if(empty($var)) | |
self::$_output.='array()'; | |
else | |
{ | |
$keys=array_keys($var); | |
$spaces=str_repeat(' ',$level*4); | |
self::$_output.="array\n".$spaces.'('; | |
foreach($keys as $key) | |
{ | |
self::$_output.="\n".$spaces." [$key] => "; | |
self::$_output.=self::dumpInternal($var[$key],$level+1); | |
} | |
self::$_output.="\n".$spaces.')'; | |
} | |
break; | |
case 'object': | |
if(($id=array_search($var,self::$_objects,true))!==false) | |
self::$_output.=get_class($var).'#'.($id+1).'(...)'; | |
else if(self::$_depth<=$level) | |
self::$_output.=get_class($var).'(...)'; | |
else | |
{ | |
$id=array_push(self::$_objects,$var); | |
$className=get_class($var); | |
$members=(array)$var; | |
$keys=array_keys($members); | |
$spaces=str_repeat(' ',$level*4); | |
self::$_output.="$className#$id\n".$spaces.'('; | |
foreach($keys as $key) | |
{ | |
$keyDisplay=strtr(trim($key),array("\0"=>':')); | |
self::$_output.="\n".$spaces." [$keyDisplay] => "; | |
self::$_output.=self::dumpInternal($members[$key],$level+1); | |
} | |
self::$_output.="\n".$spaces.')'; | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment