|
| 1 | +.. index:: |
| 2 | + single: VarDumper |
| 3 | + single: Components; VarDumper |
| 4 | + |
| 5 | +The VarDumper Component |
| 6 | +======================= |
| 7 | + |
| 8 | + The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. |
| 9 | + Built on top, it provides a better ``dump()`` function, that you can use instead of ``var_dump()``, |
| 10 | + *better* meaning: |
| 11 | + |
| 12 | + - per object and resource types specialized view to e.g. filter out Doctrine noise |
| 13 | + while dumping a single proxy entity, or get more insight on opened files with |
| 14 | + ``stream_get_meta_data()``. |
| 15 | + - configurable output format: HTML, command line with colors or JSON. |
| 16 | + - ability to dump internal references, either soft ones (objects or resources) |
| 17 | + or hard ones (``=&`` on arrays or objects properties). Repeated occurrences of |
| 18 | + the same object/array/resource won't appear again and again anymore. Moreover, |
| 19 | + you'll be able to inspected the reference structure of your data. |
| 20 | + - ability to operate in the context of an output buffering handler. |
| 21 | + |
| 22 | +.. versionadded:: 2.6 |
| 23 | + The VarDumper component was introduced in Symfony 2.6. |
| 24 | + |
| 25 | +Installation |
| 26 | +------------ |
| 27 | + |
| 28 | +You can install the component in 2 different ways: |
| 29 | + |
| 30 | +* :doc:`Install it via Composer </components/using_components>` (``symfony/var-dumper`` on `Packagist`_); |
| 31 | +* Use the official Git repository (https://github.com/symfony/VarDumper). |
| 32 | + |
| 33 | +The dump() function |
| 34 | +------------------- |
| 35 | + |
| 36 | +The VarDumper component creates a global ``dump()`` function that is auto-configured out of the box: |
| 37 | +HTML or CLI output is automatically selected based on the current PHP SAPI. |
| 38 | + |
| 39 | +``dump()`` is just a thin wrapper for ``\Symfony\Component\VarDumper\VarDumper::dump()`` so can you also use it directly. |
| 40 | +You can change the behavior of this function by calling ``\Symfony\Component\VarDumper\VarDumper::setHandler($callable)``: |
| 41 | +calls to ``dump()`` will then be forwarded to the ``$callable`` given as first argument. |
| 42 | + |
| 43 | +Advanced usage |
| 44 | +-------------- |
| 45 | + |
| 46 | +Cloners |
| 47 | +~~~~~~~ |
| 48 | + |
| 49 | +A cloner is used to create an intermediate representation of any PHP variable. |
| 50 | +Its output is a Data object that wraps this representation. |
| 51 | +A cloner also applies limits when creating the representation, so that the corresponding |
| 52 | +Data object represents only a subset of the cloned variable. |
| 53 | + |
| 54 | +You can create a Data object this way:: |
| 55 | + |
| 56 | + $cloner = new PhpCloner(); |
| 57 | + $data = $cloner->cloneVar($myVar); |
| 58 | + |
| 59 | +Before cloning, you can configure the limits with:: |
| 60 | + |
| 61 | + $cloner->setMaxItems($number); |
| 62 | + $cloner->setMaxString($number); |
| 63 | + |
| 64 | +These limits will be applied when calling ``->cloneVar()`` afterwise. |
| 65 | + |
| 66 | +Casters |
| 67 | +~~~~~~~ |
| 68 | + |
| 69 | +Objects and resources nested in a PHP variable are casted to arrays in the intermediate Data representation. |
| 70 | +You can tweak the array representation for each object/resource by hooking a Caster into this process. |
| 71 | +The component already has a many casters for base PHP classes and other common classes. |
| 72 | + |
| 73 | +If you want to build your how Caster, you can register one before cloning a PHP variable. |
| 74 | +Casters are registered either using a Cloner's constructor or its ``addCasters()`` method:: |
| 75 | + |
| 76 | + $myCasters = array(...); |
| 77 | + $cloner = new PhpCloner($myCasters); |
| 78 | + |
| 79 | +or:: |
| 80 | + |
| 81 | + $cloner->addCasters($myCasters); |
| 82 | + |
| 83 | +The provided ``$myCasters`` argument is an array that maps a class, an interface or a resource type to a callable:: |
| 84 | + |
| 85 | + $myCasters = array( |
| 86 | + 'FooClass' => $myFooClassCallableCaster, |
| 87 | + ':bar resource' => $myBarResourceCallableCaster, |
| 88 | + ); |
| 89 | + |
| 90 | +As you can notice, resource types are prefixed by a ``:`` to prevent colliding with a class name. |
| 91 | + |
| 92 | +One and only one caster can be registered per resource type/class/interface. |
| 93 | +But because an object has one main class and potentially many parent classes or interfaces, |
| 94 | +many casters can be applied to one object. In this case, casters are called one after the other, |
| 95 | +starting from casters bound to the interfaces, the parents classes and then the main class. |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + Although you can, it is best advised not to alter the state of an object while casting it in a Caster. |
| 100 | + |
| 101 | +Dumpers |
| 102 | +~~~~~~~ |
| 103 | + |
| 104 | +.. _Packagist: https://packagist.org/packages/symfony/var-dumper |
0 commit comments