Recreating Laravel dd and dump functions without composer

Posted by Andy Huggins on November 28, 2016

These are two really simple functions, `dd` and `dump`, but it's because they are so simple that I think they seem so handy.

I've written about them before, but now I find myself on a non-Laravel project, and I really want them to help in debugging things in the application.

Added little hiccup, this project doesn't use Composer, so I could normally add the Dumper that Laravel uses, and add the `dd` and `dump` functions. So now what to do?

Well, since they are so simple, you can recreate them pretty quickly and easily. Something like this:

I wanted to keep the ability to pass any number of arguments feature, since I find that helpful to pass a string to tell me what I am seeing. I also like to dump through out code sometimes to see what something is at a given point, and sometimes I dump or dd a few variables in order to see more when debugging. This feature is why I used `func_get_args()` instead of setting any parameters.

I quickly wrote a first draft of these using a `foreach` loop, but recently reread Adam Wathan's Refactoring to Collections and wanted to try and not use a loop. I ended up with a different set of functions because I was trying to be DRY. Then I looked and couldn't really see a reason to not keep it as simple as the ones posted above.

This is a good reminder to write the simplest quickest code, then take a moment and consider a new technique, refactor if you can, then pause and think if it's really as simple as it could be. What I ended up with, I think, is some very simple functions that I can add to any project and they should not have any issues working.

I do prefer not having a loop, cause that was a bit of a mess, but in this case, trying to be DRY...I was trying to reference `dump` from `dd` and it just seemed a little more complicated than just rewriting the single `array_map` line in both functions. So I just left with the `array_map` line repeated, and it's fine.

This version does not allow for the nice "tree-like" navigation that the Dumper class provides in the Laravel one, but in a pinch, these will work and sure beats typing out `echo "

";var_dump($something);echo "

";` or `print_r` every time I want to see a variable.