Is there a Pretty Print Stack Dump?

后端 未结 8 1856
半阙折子戏
半阙折子戏 2021-02-07 17:41

Let\'s face it, debug_backtrace() output is not very pretty. Did anyone code a wrapper?

And what\'s your favourite pretty var_dump() (which is

8条回答
  •  你的背包
    2021-02-07 18:08

    My favorite var_dump snippet is one I made years ago and have been working on perfecting ever since. I know there are lib's out there that create really pretty fancy dumps with accordion menus and all, but I just want a simple layout, easy to read, maybe a little HTML, and as portable as a single code-snippet method can be. Thus my function:

    function preDump() {    //  use string "noEcho" to just get a string return only
        $args = func_get_args();
        $doEcho = TRUE; $sb;
        if ($args) {
            $sb = '
    preDump: '.count($args).' Parameters Found.'; foreach (func_get_args() as $arg) { if (gettype($arg) == 'string') if ($arg == 'noEcho') { $doEcho = FALSE; $sb = preg_replace('/(preDump: )[0-9]+/', 'preDump: '.(count($args)-1), $sb); continue; } $sb .= '

    '; $sb .= json_encode($arg); break; case "string": $sb .= ' data-dump="echo">

    gettype('.gettype($arg).')

    '; $sb .= $arg; break; default: $sb .= ' data-dump="var_dump"'; if (is_object($arg)) $sb .= 'data-class="'.get_class($arg).'"'; $sb .= '>

    gettype('.gettype($arg).')'; if (is_object($arg)) $sb .= ' ['.get_class($arg).']'; $sb .= '

    '; ob_start(); var_dump($arg); $sb .= ob_get_clean(); if (ob_get_length()) ob_end_clean(); } $sb .= '

    '; } $sb .= '
    '; } else { $sb = '
    preDump: [ERROR]

    No Parameters Found

    '; } if ($doEcho) echo($sb); return $sb; }

    Use is extremely simple. It takes infinite parameters. Also, it shows everything within simple fieldsets for each preDump called, as well as separating each parameter into its own pre tag, thus making it clean and easy to read. Each pre tag also contains a header showing the gettype of each parameter, and, if it's an object, it will also show the class name.

    Use as easy as var_dump();

    preDump(TRUE, 101, 'this is a string', array( 'array', 'here' ), (object)array ( 'this' => 'is', 'an' => 'object' ), $someXMLvariable);
    

    You can also use it to get the dump as a simple string and then echo when you see fit:

    $bob = preDump($someParam1, $someParam2, 'noEcho'); // 'noEcho' causes it to return as string only
    

提交回复
热议问题