WARNING: You are browsing the documentation for Symfony 2.6 which is not maintained anymore. Consider upgrading your projects to Symfony 4.1.

The VarDumper Component

2.6 version

The VarDumper Component

The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. Built on top, it provides a better dump() function that you can use instead of var_dump.

New in version 2.6: The VarDumper component was introduced in Symfony 2.6.

Installation

You can install the component in 2 different ways:

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
require __DIR__.'/vendor/autoload.php';
// create a variable, which could be anything!
$someVar = '...';

dump($someVar);

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.

Note

If you want to catch the dump output as a string, please read the advanced documentation 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 to have the latest bug fixes.

DebugBundle and Twig Integration

The DebugBundle allows greater integration of the component into the Symfony full stack framework. It is enabled by default in the dev and test environment of the standard edition since version 2.6.

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 can not be displayed because you e.g. called die/exit 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 thus may or not be suited to your use case (e.g. you shouldn't use it in an HTML attribute or a <script> tag).

By default for nested variables, dumps are limited to a subset of their original value. You can configure the limits in terms of:

  • maximum number of items to dump,
  • maximum string length before truncation.

Since dumping into the toolbar is not always possible - e.g. when working on a JSON API - you can have an alternate output destination for dumps. This is configurable with the debug.dump_destination option, that you can typically set to php://stderr.

  • YAML
    1
    2
    3
    4
    debug:
       max_items: 250
       max_string_length: -1
       dump_destination: ~
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/debug"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/debug http://symfony.com/schema/dic/debug/debug-1.0.xsd">
    
        <config max-items="250" max-string-length="-1" dump-destination="null" />
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    $container->loadFromExtension('debug', array(
       'max_items' => 250,
       'max_string_length' => -1,
       'dump_destination' => null,
    ));
    

Dump Examples and Output

For simple variables, reading the output should be straightforward. Here are some examples showing first a variable defined in PHP, then its dump representation:

1
2
3
4
5
6
7
8
$var = array(
    'a simple string' => "in an array of 5 elements",
    'a float' => 1.0,
    'an integer' => 1,
    'a boolean' => true,
    'an empty array' => array(),
);
dump($var);
../../_images/01-simple.png

Note

The gray arrow is a toggle button for hiding/showing children of nested structures.

1
2
3
4
5
6
7
$var = "This is a multi-line string.\n";
$var .= "Hovering a string shows its length.\n";
$var .= "The length of UTF-8 strings is counted in terms of UTF-8 characters.\n";
$var .= "Non-UTF-8 strings length are counted in octet size.\n";
$var .= "Because of this `\xE9` octet (\\xE9),\n";
$var .= "this string is not UTF-8 valid, thus the `b` prefix.\n";
dump($var);
../../_images/02-multi-line-str.png
1
2
3
4
5
6
7
8
9
class PropertyExample
{
    public $publicProperty = 'The `+` prefix denotes public properties,';
    protected $protectedProperty = '`#` protected ones and `-` private ones.';
    private $privateProperty = 'Hovering a property shows a reminder.';
}

$var = new PropertyExample();
dump($var);
../../_images/03-object.png

Note

#14 is the internal object handle. It allows comparing two consecutive dumps of the same object.

1
2
3
4
5
6
7
8
class DynamicPropertyExample
{
    public $declaredProperty = 'This property is declared in the class definition';
}

$var = new DynamicPropertyExample();
$var->undeclaredProperty = 'Runtime added dynamic properties have `"` around their name.';
dump($var);
../../_images/04-dynamic-property.png
1
2
3
4
5
6
7
class ReferenceExample
{
    public $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
}
$var = new ReferenceExample();
$var->aCircularReference = $var;
dump($var);
../../_images/05-soft-ref.png
1
2
3
4
5
6
7
8
$var = new \ErrorException(
    "For some objects, properties have special values\n"
    ."that are best represented as constants, like\n"
    ."`severity` below. Hovering displays the value (`2`).\n",
    0,
    E_WARNING
);
dump($var);
../../_images/06-constants.png
1
2
3
4
5
6
7
8
$var = array();
$var[0] = 1;
$var[1] =& $var[0];
$var[1] += 1;
$var[2] = array("Hard references (circular or sibling)");
$var[3] =& $var[2];
$var[3][] = "are dumped using `&number` prefixes.";
dump($var);
../../_images/07-hard-ref.png
1
2
3
4
5
$var = new \ArrayObject();
$var[] = "Some resources and special objects like the current";
$var[] = "one are sometimes best represented using virtual";
$var[] = "properties that describe their internal state.";
dump($var);
../../_images/08-virtual-property.png
1
2
3
4
5
6
7
8
$var = new AcmeController(
    "When a dump goes over its maximum items limit,\n"
    ."or when some special objects are encountered,\n"
    ."children can be replaced by an ellipsis and\n"
    ."optionally followed by a number that says how\n"
    ."many have been removed; `9` in this case.\n"
);
dump($var);
../../_images/09-cut.png

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.