Created
March 17, 2014 09:45
-
-
Save alex-cory/9596575 to your computer and use it in GitHub Desktop.
Here my little contribution for a simple yet handy debug function. Stolen from the php documentation. :)
This file contains 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 | |
/** | |
* dbug (mixed $expression [, mixed $expression [, $... ]]) | |
* Author : dcz | |
* Feel free to use as you wish at your own risk ;-) | |
*/ | |
function dbug() { | |
static $output = '', $doc_root; | |
$args = func_get_args(); | |
if (!empty($args) && $args[0] === 'print') { | |
$_output = $output; | |
$output = ''; | |
return $_output; | |
} | |
// do not repeat the obvious (matter of taste) | |
if (!isset($doc_root)) { | |
$doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']); | |
} | |
$backtrace = debug_backtrace(); | |
// you may want not to htmlspecialchars here | |
$line = htmlspecialchars($backtrace[0]['line']); | |
$file = htmlspecialchars(str_replace(array('\\', $doc_root), array('/', ''), $backtrace[0]['file'])); | |
$class = !empty($backtrace[1]['class']) ? htmlspecialchars($backtrace[1]['class']) . '::' : ''; | |
$function = !empty($backtrace[1]['function']) ? htmlspecialchars($backtrace[1]['function']) . '() ' : ''; | |
$output .= "<b>$class$function =>$file #$line</b><pre>"; | |
ob_start(); | |
foreach ($args as $arg) { | |
var_dump($arg); | |
} | |
$output .= htmlspecialchars(ob_get_contents(), ENT_COMPAT, 'UTF-8'); | |
ob_end_clean(); | |
$output .= '</pre>'; | |
} | |
?> | |
usage : | |
<?php | |
dbug($scalar, $array, $object, $resource, CONSTANT); | |
//.. | |
dbug($other); | |
//.. | |
echo dbug('print'); // actually output the result of all previous calls | |
// looks like : | |
// class::method() =>/path/from/doc/root/file.php #line | |
// var_dump result | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment