DEV Community

Freek Van der Herten
Freek Van der Herten

Posted on • Originally published at freek.dev on

1 1

★ 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!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay