Posted By

Snaver on 07/12/10


Tagged

class php debug


Versions (?)

Debug Class


 / Published in: PHP
 

The explain() function is a nice replacement for native functions like var_dump() etc. It can explain all type of variables in a better format. It returns the output and if $echo is true, which is the default value, it also prints to the screen.

dbgWriteDb() -> This one obviously writes the input value to a table including two columns : an id and a message of type text.

And the showMysqlError(). You will need the function mysql_error() frequently in case of errors. And there can be times that it is impossible to output the error to the screen. One example is a custom class that uses the database as the session storage instead of the default file system. Since some session job have to happen before any output sent, if there is a database error, you cannot see it on your screen. Instead, you can direct it to a database table. This function writes the error either to the screen (default) or to the database.

Examples:

PHP Code: debug::explain($SERVER); prints all $SERVER variables to the screen in a nice format.

PHP Code: debug::showMysqlError('db'); records the last MySQL error to the database.

  1. class debug
  2. {
  3.     function explain($var, $echo = true)
  4.     {
  5.         $out = '<table border="1" cellspacing="0" cellpadding="3" style="text-align:left;margin:1em;">';
  6.         $type = gettype($var);
  7.  
  8.         switch ($type)
  9.         {
  10.             case 'boolean':
  11.                 $var = ($var?'true':'false');
  12.             case 'integer':
  13.             case 'double':
  14.             case 'NULL' :
  15.             case 'string':
  16.                 $out .= '<tr><th style="color:#a71">'.$type.' :</th><td>'.htmlspecialchars($var).'</td></tr>';
  17.                 break;
  18.             case 'array':
  19.                 $out .= '
  20.                     <tr>
  21.                         <th colspan="2" style="color:#a71">ARRAY</th>
  22.                     </tr>
  23.                     <tr style="color:#196">
  24.                         <th>KEY</th>
  25.                         <th>VALUE</th>
  26.                     </tr>';
  27.                 foreach ( $var as $key => $value )
  28.                 {
  29.                     $out .= '<tr><th>'.htmlspecialchars($key).'</th><td>'.nl2br(htmlspecialchars($value)).'</td></tr>';
  30.                 }
  31.                 break;
  32.             case 'unknown type':
  33.                 $out .= '<tr><th colspan="2" style="color:#a71">'.$type.' :</th></tr>';
  34.                 break;
  35.             case 'object':
  36.                 $className = get_class($var);
  37.                 $vars = get_class_vars($className);
  38.                 $methods = get_class_methods($className);
  39.                 $out .= '
  40.                 <tr>
  41.                     <th colspan="2" style="color:#a71; text-align:center;">'.$type.'</th>
  42.                 </tr>
  43.                 <tr>
  44.                     <th>instance of :</th>
  45.                     <td>'.$className.'</td>
  46.                 </tr>
  47.                 <tr>
  48.                     <th colspan="2" style="color:#966;">PROPERTIES :</th>
  49.                 </tr>
  50.                 <tr>
  51.                     <td colspan="2">'.(empty($vars)?'none':debug::explain($vars,false)).'</td>
  52.                 </tr>
  53.                 <tr>
  54.                     <th colspan="2" style="color:#36b;">METHODS :</th>
  55.                 </tr>
  56.                 <tr>
  57.                     <td colspan="2">'.(empty($methods)?'none':debug::explain($methods,false)).'</td>
  58.                 </tr>
  59.                 ';
  60.                 break;
  61.             case 'resource':
  62.                 $out .= '
  63.                 <tr>
  64.                     <th colspan="2" style="color:#a71;text-align:center;">'.$type.'</th>
  65.                 </tr>
  66.                 <tr>
  67.                     <th>'.get_resource_type($var).' :</th>
  68.                     <td>'.$var.'</td>
  69.                 </tr>';
  70.                 break;
  71.         }
  72.  
  73.         if ( $type == 'string' && function_exists($var) )
  74.             $out .= '<tr><th colspan="2" style="color:#36b">also a function name</th></tr>';
  75.  
  76.         if ( $type == 'string' && class_exists($var) )
  77.             $out .= '<tr><th colspan="2" style="color:#36b">also a class name</th></tr>';
  78.  
  79.         $out .= '</table>';
  80.  
  81.         if ( $echo )
  82.             echo $out;
  83.  
  84.         return $out;
  85.     }
  86.  
  87.     function dbgWriteDb($msg)
  88.     {
  89.         db::q( 'insert into debug values(null, %se)', $msg );
  90.     }
  91.  
  92.     function showMysqlError($where = 'screen')
  93.     {
  94.         $err = mysql_error();
  95.         if ( $where == 'db' )
  96.             debug::dbgWriteDb('last mysql error : '.$err);
  97.         else
  98.             echo $err;
  99.     }
  100. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Kehet on December 24, 2010

instead case 'array': $out .= '

    ARRAY


    KEY
    VALUE
        ';
    foreach ( $var as $key => $value )
    {
        $out .= ''.htmlspecialchars($key).''.nl2br(htmlspecialchars($value)).'';
    }
    break;

use this to handle multi level arrays case 'array': $out .= '

    ARRAY


    KEY
    VALUE
';
    foreach ( $var as $key => $value )
    {
        $out .= ''.htmlspecialchars($key).''.(is_string($value)?nl2br(htmlspecialchars($value)):debug::explain($value,false)).'';
    }
    break;
Posted By: Kehet on December 24, 2010

damn that markdown >:/

You need to login to post a comment.