DEV Community

Freek Van der Herten
Freek Van der Herten

Posted on • Originally published at freek.dev on

★ Symfony now has an improved dump function

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!

Top comments (0)