Skip to content

The VarDumper Component

Edit this page

The VarDumper component provides mechanisms for extracting the state out of any PHP variables. Built on top, it provides a better dump() function that you can use instead of var_dump.

Installation

1
$ composer require --dev symfony/var-dumper

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Note

If using it inside a Symfony application, make sure that the DebugBundle has been installed (or run composer require --dev symfony/debug-bundle to install it).

The dump() Function

The VarDumper component creates a global dump() function that you can use instead of e.g. var_dump. By using it, you'll gain:

  • Per object and resource types specialized view to e.g. filter out Doctrine internals while dumping a single proxy entity, or get more insight on opened files with stream_get_meta_data;
  • Configurable output formats: HTML or colored command line output;
  • Ability to dump internal references, either soft ones (objects or resources) or hard ones (=& on arrays or objects properties). Repeated occurrences of the same object/array/resource won't appear again and again anymore. Moreover, you'll be able to inspect the reference structure of your data;
  • Ability to operate in the context of an output buffering handler.

For example:

1
2
3
4
5
6
7
8
9
require __DIR__.'/vendor/autoload.php';

// create a variable, which could be anything!
$someVar = ...;

dump($someVar);

// dump() returns the passed value, so you can dump an object and keep using it
dump($someObject)->someMethod();

By default, the output format and destination are selected based on your current PHP SAPI:

  • On the command line (CLI SAPI), the output is written on STDOUT. This can be surprising to some because this bypasses PHP's output buffering mechanism;
  • On other SAPIs, dumps are written as HTML in the regular output.

Tip

You can also select the output format explicitly defining the VAR_DUMPER_FORMAT environment variable and setting its value to either html, cli or server.

Note

If you want to catch the dump output as a string, please read the advanced section which contains examples of it. You'll also learn how to change the format or redirect the output to wherever you want.

Tip

In order to have the dump() function always available when running any PHP code, you can install it globally on your computer:

  1. Run composer global require symfony/var-dumper;
  2. Add auto_prepend_file = ${HOME}/.composer/vendor/autoload.php to your php.ini file;
  3. From time to time, run composer global update symfony/var-dumper to have the latest bug fixes.

Tip

The VarDumper component also provides a dd() ("dump and die") helper function. This function dumps the variables using dump() and immediately ends the execution of the script (using exit).

The Dump Server

The dump() function outputs its contents in the same browser window or console terminal as your own application. Sometimes mixing the real output with the debug output can be confusing. That's why this component provides a server to collect all the dumped data.

Start the server with the server:dump command and whenever you call dump(), the dumped data won't be displayed in the output but sent to that server, which outputs it to its own console or to an HTML file:

1
2
3
4
5
6
# displays the dumped data in the console:
$ php bin/console server:dump
  [OK] Server listening on tcp://0.0.0.0:9912

# stores the dumped data in a file using the HTML format:
$ php bin/console server:dump --format=html > dump.html

Inside a Symfony application, the output of the dump server is configured with the dump_destination option of the debug package:

1
2
3
# config/packages/debug.yaml
debug:
   dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"

Outside a Symfony application, use the ServerDumper class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Dumper\ServerDumper;
use Symfony\Component\VarDumper\VarDumper;

$cloner = new VarCloner();
$fallbackDumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
$dumper = new ServerDumper('tcp://127.0.0.1:9912', $fallbackDumper, [
    'cli' => new CliContextProvider(),
    'source' => new SourceContextProvider(),
]);

VarDumper::setHandler(function (mixed $var) use ($cloner, $dumper): ?string {
    return $dumper->dump($cloner->cloneVar($var));
});

Note

The second argument of ServerDumper is a DataDumperInterface instance used as a fallback when the server is unreachable. The third argument is the context providers, which allow you to gather some info about the context in which the data was dumped. The built-in context providers are: cli, request and source.

Then you can use the following command to start a server out-of-the-box:

1
2
$ ./vendor/bin/var-dump-server
  [OK] Server listening on tcp://127.0.0.1:9912

Configuring the Dump Server with Environment Variables

If you prefer to not modify the application configuration (e.g. to quickly debug a project given to you) use the VAR_DUMPER_FORMAT env var.

First, start the server as usual:

1
$ ./vendor/bin/var-dump-server

Then, run your code with the VAR_DUMPER_FORMAT=server env var by configuring this value in the .env file of your application. For console commands, you can also define this env var as follows:

1
$ VAR_DUMPER_FORMAT=server [your-cli-command]

Note

The host used by the server format is the one configured in the VAR_DUMPER_SERVER env var or 127.0.0.1:9912 if none is defined. If you prefer, you can also configure the host in the VAR_DUMPER_FORMAT env var like this: VAR_DUMPER_FORMAT=tcp://127.0.0.1:1234.

DebugBundle and Twig Integration

The DebugBundle allows greater integration of this component into Symfony applications.

Since generating (even debug) output in the controller or in the model of your application may just break it by e.g. sending HTTP headers or corrupting your view, the bundle configures the dump() function so that variables are dumped in the web debug toolbar.

But if the toolbar cannot be displayed because you e.g. called die()/exit()/dd() or a fatal error occurred, then dumps are written on the regular output.

In a Twig template, two constructs are available for dumping a variable. Choosing between both is mostly a matter of personal taste, still:

  • {% dump foo.bar %} is the way to go when the original template output shall not be modified: variables are not dumped inline, but in the web debug toolbar;
  • on the contrary, {{ dump(foo.bar) }} dumps inline and may or not be suited to your use case (e.g. you shouldn't use it in an HTML attribute or a
    This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
    TOC
      Version