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.
class debug { function explain($var, $echo = true) { $out = '<table border="1" cellspacing="0" cellpadding="3" style="text-align:left;margin:1em;">'; switch ($type) { case 'boolean': $var = ($var?'true':'false'); case 'integer': case 'double': case 'NULL' : case 'string': break; case 'array': $out .= ' <tr> <th colspan="2" style="color:#a71">ARRAY</th> </tr> <tr style="color:#196"> <th>KEY</th> <th>VALUE</th> </tr>'; foreach ( $var as $key => $value ) { } break; case 'unknown type': $out .= '<tr><th colspan="2" style="color:#a71">'.$type.' :</th></tr>'; break; case 'object': $out .= ' <tr> <th colspan="2" style="color:#a71; text-align:center;">'.$type.'</th> </tr> <tr> <th>instance of :</th> <td>'.$className.'</td> </tr> <tr> <th colspan="2" style="color:#966;">PROPERTIES :</th> </tr> <tr> </tr> <tr> <th colspan="2" style="color:#36b;">METHODS :</th> </tr> <tr> </tr> '; break; case 'resource': $out .= ' <tr> <th colspan="2" style="color:#a71;text-align:center;">'.$type.'</th> </tr> <tr> <td>'.$var.'</td> </tr>'; break; } $out .= '<tr><th colspan="2" style="color:#36b">also a function name</th></tr>'; $out .= '<tr><th colspan="2" style="color:#36b">also a class name</th></tr>'; $out .= '</table>'; if ( $echo ) echo $out; return $out; } function dbgWriteDb($msg) { db::q( 'insert into debug values(null, %se)', $msg ); } function showMysqlError($where = 'screen') { if ( $where == 'db' ) debug::dbgWriteDb('last mysql error : '.$err); else echo $err; } }
Comments
Subscribe to comments
You need to login to post a comment.

instead case 'array': $out .= '
use this to handle multi level arrays case 'array': $out .= '
damn that markdown >:/