5
5
Advanced Usage of the VarDumper Component
6
6
=========================================
7
7
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
9
9
:method: `VarDumper::dump() <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> `.
10
10
You 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
+ });
13
29
14
30
Cloners
15
- ~~~~~~~
31
+ -------
16
32
17
33
A cloner is used to create an intermediate representation of any PHP variable.
18
34
Its output is a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
@@ -21,18 +37,24 @@ object that wraps this representation.
21
37
You can create a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
22
38
object this way::
23
39
40
+ use Symfony\Component\VarDumper\Cloner\VarCloner;
41
+
24
42
$cloner = new VarCloner();
25
43
$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);
26
47
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
28
49
corresponding 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 `,
30
51
you can configure these limits:
31
52
32
53
* :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;
36
58
* :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::setMaxString `
37
59
configures the maximum number of characters that will be cloned before
38
60
cutting overlong strings;
@@ -45,7 +67,7 @@ method:
45
67
46
68
* the first ``$maxDepth `` argument allows limiting dumps in the depth dimension,
47
69
* 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
49
71
internal objects' handles for sparser output,
50
72
* but unlike the previous limits on cloners that remove data on purpose,
51
73
these can be changed back and forth before dumping since they do not affect
@@ -54,11 +76,11 @@ method:
54
76
.. note ::
55
77
56
78
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.
59
81
60
82
Dumpers
61
- ~~~~~~~
83
+ -------
62
84
63
85
A dumper is responsible for outputting a string representation of a PHP variable,
64
86
using a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object as input.
@@ -70,14 +92,17 @@ for optionally colored command line output.
70
92
71
93
For example, if you want to dump some ``$variable ``, just do::
72
94
95
+ use Symfony\Component\VarDumper\Cloner\VarCloner;
96
+ use Symfony\Component\VarDumper\Dumper\CliDumper;
97
+
73
98
$cloner = new VarCloner();
74
99
$dumper = new CliDumper();
75
100
76
101
$dumper->dump($cloner->cloneVar($variable));
77
102
78
103
By using the first argument of the constructor, you can select the output
79
104
stream 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
81
106
stream (resource or URL) is acceptable.
82
107
83
108
Instead of a stream destination, you can also pass it a ``callable `` that
@@ -90,6 +115,9 @@ method or the second argument of the
90
115
91
116
For example, to get a dump as a string in a variable, you can do::
92
117
118
+ use Symfony\Component\VarDumper\Cloner\VarCloner;
119
+ use Symfony\Component\VarDumper\Dumper\CliDumper;
120
+
93
121
$cloner = new VarCloner();
94
122
$dumper = new CliDumper();
95
123
$output = '';
@@ -107,7 +135,10 @@ For example, to get a dump as a string in a variable, you can do::
107
135
108
136
// $output is now populated with the dump representation of $variable
109
137
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;
111
142
112
143
cloner = new VarCloner();
113
144
$dumper = new CliDumper();
@@ -128,9 +159,9 @@ them from re-implementing the logic required to walk through a
128
159
:class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object's internal structure.
129
160
130
161
Casters
131
- ~~~~~~~
162
+ -------
132
163
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
134
165
intermediate :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
135
166
representation. You can tweak the array representation for each object/resource
136
167
by 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
140
171
a PHP variable. Casters are registered using either a Cloner's constructor
141
172
or its ``addCasters() `` method::
142
173
174
+ use Symfony\Component\VarDumper\Cloner\VarCloner;
175
+
143
176
$myCasters = array(...);
144
177
$cloner = new VarCloner($myCasters);
145
178
@@ -172,7 +205,7 @@ being cloned in an array. They are callables that accept four arguments:
172
205
* an array modelled for objects after PHP's native ``(array) `` cast operator,
173
206
* a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Stub ` object
174
207
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.
176
209
177
210
Here is a simple caster not doing anything::
178
211
@@ -186,18 +219,18 @@ Here is a simple caster not doing anything::
186
219
For objects, the ``$array `` parameter comes pre-populated using PHP's native
187
220
``(array) `` casting operator or with the return value of ``$object->__debugInfo() ``
188
221
if 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.
190
223
191
224
When 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
196
229
properties not in the class declaration).
197
230
198
231
.. note ::
199
232
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
201
234
while casting it in a Caster.
202
235
203
236
.. tip ::
0 commit comments