Together with Marcel Pociot and our colleagues at Beyond Code and Spatie, I'm currently building Flare, a paid service which will be revealed at Laracon EU. Together with the service we'll release a package that will change the way you will work with Laravel. To stay in the loop subscribe to our mailinglist at https://flareapp.io

Symfony now has an improved dump function

Original – by Freek Van der Herten – 2 minute read

Let's talk a little bit about Symfony's dump function. It's part of their VarDumper component. The function can dump a variable to the screen or browser in a nicer format than PHP's native var_dump. In the recently released Symfony 3.4 and Symfony 4 the function got a nice little improvement that I'd like to highlight in this post.

You probably already know that you can dump stuff like this:

dump($object);

Nothing too fancy. But if you did this in previous versions of the framework it would blow up:

// dump didn't return anything so method is getting called on null ☠️
dump($object)->method();

I was quite annoyed with this so I submitted a PR to Symfony that let dump return the things that it was given. So now you can just wrap the function around any variable even if there are methods being called on it.

// in Symfony 3.4 and 4.x this just works ????
dump($object)->method();

When dumping multiple things, they all get returned. So you could dump all arguments passed to a function like this.

$object->method(...dump($arg1, $arg2, $arg3));

If you are using Laravel you'll be happy to know that Laravel just uses Symfony's dump function, so you can make use of this little improvement in Laravel too.

Happy dumping! ????

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.