55Advanced Usage of the VarDumper Component
66=========================================
77
8- ``dump() `` function is just a thin wrapper and a more convenient way to call
8+ The ``dump() `` function is just a thin wrapper and a more convenient way to call
99:method: `VarDumper::dump() <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> `.
1010You can change the behavior of this function by calling
11- :method: `VarDumper::setHandler($callable) <Symfony\\ Component\\ VarDumper\\ VarDumper::setHandler> `:
12- calls to ``dump() `` will then be forwarded to ``$callable ``.
11+ :method: `VarDumper::setHandler($callable) <Symfony\\ Component\\ VarDumper\\ VarDumper::setHandler> `.
12+ Calls to ``dump() `` will then be forwarded to ``$callable ``.
13+
14+ By adding a handler, you can customize the `Cloners `_, `Dumpers `_ and `Casters `_
15+ explained below. A simple implementation of a handler function might look
16+ like this::
17+
18+ use Symfony\Component\VarDumper\VarDumper;
19+ use Symfony\Component\VarDumper\Cloner\VarCloner;
20+ use Symfony\Component\VarDumper\Dumper\CliDumper;
21+ use Symfony\Component\VarDumper\Dumper\HtmlDumper;
22+
23+ VarDumper::setHandler(function($var) {
24+ $cloner = new VarCloner();
25+ $dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
26+
27+ $dumper->dump($cloner->cloneVar($var));
28+ });
1329
1430Cloners
15- ~~~~~~~
31+ -------
1632
1733A cloner is used to create an intermediate representation of any PHP variable.
1834Its output is a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
@@ -21,18 +37,24 @@ object that wraps this representation.
2137You can create a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
2238object this way::
2339
40+ use Symfony\Component\VarDumper\Cloner\VarCloner;
41+
2442 $cloner = new VarCloner();
2543 $data = $cloner->cloneVar($myVar);
44+ // this is commonly then passed to the dumpe
45+ // see the example at the top of this page
46+ // $dumper->dump($data);
2647
27- A cloner also applies limits when creating this representation, so that the
48+ A cloner also applies limits when creating the representation, so that the
2849corresponding Data object could represent only a subset of the cloned variable.
29- Before :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::cloneVar `,
50+ Before calling :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::cloneVar `,
3051you can configure these limits:
3152
3253* :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::setMaxItems `
33- configures the maximum number of items that will be cloned *past the first
34- nesting level *. Items are counted using a breadth-first algorithm so that
35- lower level items have higher priority than deeply nested items;
54+ configures the maximum number of items that will be cloned
55+ *past the first nesting level *. Items are counted using a breadth-first
56+ algorithm so that lower level items have higher priority than deeply nested
57+ items;
3658* :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::setMaxString `
3759 configures the maximum number of characters that will be cloned before
3860 cutting overlong strings;
@@ -45,7 +67,7 @@ method:
4567
4668* the first ``$maxDepth `` argument allows limiting dumps in the depth dimension,
4769* the second ``$maxItemsPerDepth `` limits the number of items per depth level,
48- * and the last ``$useRefHandles `` defaults to ``true `` but allows removing
70+ * and the last ``$useRefHandles `` defaults to ``true ``, but allows removing
4971 internal objects' handles for sparser output,
5072* but unlike the previous limits on cloners that remove data on purpose,
5173 these can be changed back and forth before dumping since they do not affect
@@ -54,11 +76,11 @@ method:
5476.. note ::
5577
5678 When no limit is applied, a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
57- object is as accurate as the native :phpfunction: `serialize ` function
58- and thus could have a wider purpose than strictly dumping for debugging.
79+ object is as accurate as the native :phpfunction: `serialize ` function,
80+ and thus could be for purposes beyond dumping for debugging.
5981
6082Dumpers
61- ~~~~~~~
83+ -------
6284
6385A dumper is responsible for outputting a string representation of a PHP variable,
6486using a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object as input.
@@ -70,14 +92,17 @@ for optionally colored command line output.
7092
7193For example, if you want to dump some ``$variable ``, just do::
7294
95+ use Symfony\Component\VarDumper\Cloner\VarCloner;
96+ use Symfony\Component\VarDumper\Dumper\CliDumper;
97+
7398 $cloner = new VarCloner();
7499 $dumper = new CliDumper();
75100
76101 $dumper->dump($cloner->cloneVar($variable));
77102
78103By using the first argument of the constructor, you can select the output
79104stream where the dump will be written. By default, the ``CliDumper `` writes
80- on ``php://stdout `` and the ``HtmlDumper `` on ``php://output ``, but any PHP
105+ on ``php://stdout `` and the ``HtmlDumper `` on ``php://output ``. But any PHP
81106stream (resource or URL) is acceptable.
82107
83108Instead of a stream destination, you can also pass it a ``callable `` that
@@ -90,6 +115,9 @@ method or the second argument of the
90115
91116For example, to get a dump as a string in a variable, you can do::
92117
118+ use Symfony\Component\VarDumper\Cloner\VarCloner;
119+ use Symfony\Component\VarDumper\Dumper\CliDumper;
120+
93121 $cloner = new VarCloner();
94122 $dumper = new CliDumper();
95123 $output = '';
@@ -107,7 +135,10 @@ For example, to get a dump as a string in a variable, you can do::
107135
108136 // $output is now populated with the dump representation of $variable
109137
110- An other option for doing the same could be::
138+ Another option for doing the same could be::
139+
140+ use Symfony\Component\VarDumper\Cloner\VarCloner;
141+ use Symfony\Component\VarDumper\Dumper\CliDumper;
111142
112143 cloner = new VarCloner();
113144 $dumper = new CliDumper();
@@ -128,9 +159,9 @@ them from re-implementing the logic required to walk through a
128159:class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object's internal structure.
129160
130161Casters
131- ~~~~~~~
162+ -------
132163
133- Objects and resources nested in a PHP variable are casted to arrays in the
164+ Objects and resources nested in a PHP variable are "cast" to arrays in the
134165intermediate :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
135166representation. You can tweak the array representation for each object/resource
136167by hooking a Caster into this process. The component already includes many
@@ -140,6 +171,8 @@ If you want to build your own Caster, you can register one before cloning
140171a PHP variable. Casters are registered using either a Cloner's constructor
141172or its ``addCasters() `` method::
142173
174+ use Symfony\Component\VarDumper\Cloner\VarCloner;
175+
143176 $myCasters = array(...);
144177 $cloner = new VarCloner($myCasters);
145178
@@ -172,7 +205,7 @@ being cloned in an array. They are callables that accept four arguments:
172205* an array modelled for objects after PHP's native ``(array) `` cast operator,
173206* a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Stub ` object
174207 representing the main properties of the object (class, type, etc.),
175- * true/false when the caster is called nested is a structure or not.
208+ * true/false when the caster is called nested in a structure or not.
176209
177210Here is a simple caster not doing anything::
178211
@@ -186,18 +219,18 @@ Here is a simple caster not doing anything::
186219For objects, the ``$array `` parameter comes pre-populated using PHP's native
187220``(array) `` casting operator or with the return value of ``$object->__debugInfo() ``
188221if the magic method exists. Then, the return value of one Caster is given
189- as argument to the next Caster in the chain.
222+ as the array argument to the next Caster in the chain.
190223
191224When casting with the ``(array) `` operator, PHP prefixes protected properties
192- with a ``\0*\0 `` and private ones with the class owning the property:
193- e.g. ``\0Foobar\0 `` prefixes all private properties of objects of type Foobar.
194- Casters follow this convention and add two more prefixes: ``\0~\0 `` is used
195- for virtual properties and ``\0+\0 `` for dynamic ones (runtime added
225+ with a ``\0*\0 `` and private ones with the class owning the property. For example,
226+ ``\0Foobar\0 `` will be the prefix for all private properties of objects of
227+ type Foobar. Casters follow this convention and add two more prefixes: ``\0~\0 ``
228+ is used for virtual properties and ``\0+\0 `` for dynamic ones (runtime added
196229properties not in the class declaration).
197230
198231.. note ::
199232
200- Although you can, it is best advised not to alter the state of an object
233+ Although you can, it is advised to not alter the state of an object
201234 while casting it in a Caster.
202235
203236.. tip ::
0 commit comments