Skip to content

Instantly share code, notes, and snippets.

@hiephm
Created August 18, 2016 08:24
Show Gist options
  • Save hiephm/ae0a4e401c5490677287ca2fbd9dad7b to your computer and use it in GitHub Desktop.
Save hiephm/ae0a4e401c5490677287ca2fbd9dad7b to your computer and use it in GitHub Desktop.
Debug helper functions
function __die($obj)
{
print_r($obj);die();
}
function __debug($message)
{
$file = __DIR__ . '/../var/log/dump.log';
file_put_contents($file, $message, FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
}
function __dumpException(\Exception $e, $returnAsString = false)
{
$newline = (PHP_SAPI == 'cli') ? "\n" : "<br/>\n";
$tab = (PHP_SAPI == 'cli') ? "\t" : "&nbsp;&nbsp;&nbsp;&nbsp;";
$output = '';
$output .= "Message: {$e->getMessage()}{$newline}";
foreach ($e->getTrace() as $step => $trace) {
$file = isset($trace['file']) ? $trace['file'] : '';
$line = isset($trace['line']) ? $trace['line'] : '';
$func = isset($trace['function']) ? $trace['function'] : '';
$class = isset($trace['class']) ? $trace['class'] : '';
$type = isset($trace['type']) ? $trace['type'] : '';
$args = '';
if (isset($trace['args'])) {
$items = array();
foreach ($trace['args'] as $pos => $arg) {
if ($arg === '') {
$arg = '\'\'';
} elseif ($arg === null) {
$arg = '[null]';
} elseif (is_bool($arg)) {
$arg = $arg ? '[true]' : '[false]';
} elseif (is_scalar($arg)) {
$arg = strval($arg);
} elseif (is_array($arg)) {
$arg = '[array]';
}elseif (is_object($arg)) {
$arg = sprintf('[object %s]', get_class($arg));
} else {
$arg = '[unknown]';
}
$items[] = "{$pos}:$arg";
}
$args = '(' . implode(', ', $items) . ')';
}
$output .= "#{$step} {$file} ({$line}):{$newline} {$tab} {$class}{$type}{$func}{$args}{$newline}{$newline}";
}
if ($returnAsString) {
return $output;
} else {
echo $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment