November 27th, 2012, 12:37 PM
A var_dump() & print_r() alternative
Hey guys, I couldn't find a snippet sub-forum, so I guess I'll post it here. I put together dump_r(), which I feel is a much better print_r() & var_dump() combination. It's lightweight, single-file-include, fully css-themeable and extendable for dumping additional object types. It has other features as well (see the repo link). Try it out if you'd like. I'd appreciate bug reports, feature requests, css themes, patches or general feedback. Requires PHP 5.3+, if you're on <5.3, consider this your public shaming.
there are also some additional expand/collapse features if you hold Ctrl/Shift while collapsing and expanding. They're briefly explained in the commit message here .
Comments on this post
November 28th, 2012, 09:46 AM
December 3rd, 2012, 11:25 AM
One thing this doesn't do is gracefully degrade on the command line. If I print_r to a non-browser (flat file, cli) it's going to be hell to try to chunk through your HTML. If $_SERVER['HTTP_USER_AGENT'] isn't set, maybe fall back to a plaintext version.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question
or you're a Help Vampire.
Trying to argue intelligently? Please read this.
December 3rd, 2012, 12:45 PM
yeah, it'll mess your command line up!
internally, the structure decomposition is separate from the rendering pass. so it's just a matter of writing an additional text rendering func.
the most obvious and lightweight thing that comes to mind for text dumping is to just call var_dump and maybe reformat it slightly. there's no escaping the need for all the extra clutter when you can't rely on colors. of course this path would bypass lots of goodies that the decomposed tree offers.
ultimately, there needs to be a text version. i'm a bit busy now so probably wont get around to it for some time. but feel free to submit pull requests
thanks for the feedback!
December 3rd, 2012, 01:30 PM
thinking a bit more...
once the text render method is done, the console part should be easy. but there's still no way to do something like print_r does, with explicitly returning the txt representation to write to a file.
i might change the signature a bit to account for this in conjunction with limiting the recursion depth. maybe
if $ret is set to true, it can be assumed that text is needed perhaps...
dump_r($val, $expand=1000, $depth=1000, $ret=false);
December 3rd, 2012, 03:54 PM
^^ this is now implemented ^^
as a stop-gap until i have time to write a text renderer, the lib will now fall back to plain var_dump() when in cli or when returning output.
Comments on this post
December 4th, 2012, 05:19 PM
okay, the issue was annoying me, so I found some time for this. there are some small caveats and unfinished things still on the todo list.
- multiline strings will get chopped off after the first line by the regex that handles the re-alignment after everything is buffered.
- private and protected properties are not identified as such
- keys are not enclosed in anything (for readability), so leading and trailing whitespace in string keys will not be obvious. i'm assuming this is pretty rare.
there aren't options right now to limit what does and doesnt get displayed, but the planned settings are:
- independently toggle str lengths, arr lengths, subtypes, parenths around lengths + subtypes, enclosing of string keys in single quotes
- key => value dots as horiz guides for easier readability
- more detail for recursion and reference types
that said, i tried to make it as clean and useful as possible and match the html version when possible. recursion depth limiting is supported the same way as in the html output. the var_dump and print_r versions are also included at the bottom of the example.
[UPDATE] if you want to try it out, it's up on a separate branch: https://github.com/leeoniya/dump_r.php/tree/renderTEXT
Last edited by Leeoniya; December 4th, 2012 at 07:30 PM.
Reason: mention renderTEXT branch
December 5th, 2012, 12:39 PM
the text-mode multiline issue (the only serious one) has been fixed. unlike var_dump and print_r, it also aligns the string's individual lines to the correct nesting level.
i merged the renderTEXT branch into master, so this functionality is now official.
the example at http://o-0.me/dump_r/ has also been updated to include text dumps.
January 19th, 2013, 11:51 AM
[Update] pretty significant rewrite (OOP/PSR-0). better extensibility, dumping of uniform arrays of objects or assoc arrays as record tables.