Posted By

hmmNaGut on 06/15/11


Tagged

class debug backtrace


Versions (?)

A small debug Class with backtrace


 / Published in: PHP
 

This class is useful to debug your code. I often use the debug function because i can find where my debug messages are.

I hope my function helps!

  1. class Dbg{
  2. public static $_mode = false;
  3.  
  4. /** Short hand for print_r exit; **/
  5. public function prd($d){
  6. if(!self::$_mode)
  7. return;
  8.  
  9. echo '<pre>';
  10. print_r($d);
  11. echo '</pre>';
  12. }
  13.  
  14. /** shorthand for print_r with pre **/
  15. public static function pr($d){
  16. if(!self::$_mode)
  17. return;
  18.  
  19. echo '<pre>';
  20. print_r($d);
  21. echo '</pre>';
  22. }
  23.  
  24. /**
  25. * shorthand print_r with backtrace
  26. */
  27.  
  28. public static function debug($d){
  29. if(!self::$_mode)
  30. return;
  31.  
  32. $file = 'undefined';
  33. $line = 'undefined';
  34.  
  35. $backtraceInfo = debug_backtrace();
  36.  
  37. $countTrace = count($backtraceInfo);
  38.  
  39. if($countTrace == 1){
  40. $line = $backtraceInfo[0]['line'];
  41. $file = $backtraceInfo[0]['file'];
  42. }
  43. elseif($countTrace){
  44. $line = $backtraceInfo[$countTrace -2]['line'];
  45. $file = $backtraceInfo[$countTrace -2]['file'];
  46. }
  47.  
  48. echo sprintf('<div class="debug"><strong>Zeile: %s, Datei: %s</strong><br /><pre>', $line, $file);
  49. print_r($d);
  50. echo '</pre></div>';
  51. }
  52.  
  53. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hmmNaGut on June 15, 2011

There are only 3 little functions but i think the can be really useful.

Posted By: hmmNaGut on June 15, 2011

There are only 3 little functions but i think the can be really useful.

You need to login to post a comment.