8000 [#4243] Tweaks to the new var-dumper component · symfony/symfony-docs@17c6cac · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 17c6cac

Browse files
committed
[#4243] Tweaks to the new var-dumper component
1 parent cd182f2 commit 17c6cac

File tree

2 files changed

+91
-29
lines changed

2 files changed

+91
-29
lines changed

components/var_dumper/advanced.rst

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@
55
Advanced 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>`.
1010
You can change the behavior of this function by calling
11< 8000 span class="diff-text-marker">-
: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

1430
Cloners
15-
~~~~~~~
31+
-------
1632

1733
A cloner is used to create an intermediate representation of any PHP variable.
1834
Its output is a :class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
@@ -21,18 +37,24 @@ object that wraps this representation.
2137
You can create a :class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
2238
object 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
2849
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`,
3051
you 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

6082
Dumpers
61-
~~~~~~~
83+
-------
6284

6385
A dumper is responsible for outputting a string representation of a PHP variable,
6486
using a :class:`Symfony\\Component\\VarDumper\\Cloner\\Data` object as input.
@@ -70,14 +92,17 @@ for optionally colored command line output.
7092

7193
For 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

78103
By using the first argument of the constructor, you can select the output
79104
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
81106
stream (resource or URL) is acceptable.
82107

83108
Instead of a stream destination, you can also pass it a ``callable`` that
@@ -90,6 +115,9 @@ method or the second argument of the
90115

91116
For 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

130161
Casters
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
134165
intermediate :class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
135166
representation. You can tweak the array representation for each object/resource
136167
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
140171
a PHP variable. Casters are registered using either a Cloner's constructor
141172
or 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

177210
Here is a simple caster not doing anything::
178211

@@ -186,18 +219,18 @@ Here is a simple caster not doing anything::
186219
For objects, the ``$array`` parameter comes pre-populated using PHP's native
187220
``(array)`` casting operator or with the return value of ``$object->__debugInfo()``
188221
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.
190223

191224
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
196229
properties 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::

components/var_dumper/introduction.rst

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,21 @@ use instead of e.g. :phpfunction:`var_dump`. By using it, you'll gain:
3737
reference structure of your data;
3838
* Ability to operate in the context of an output buffering handler.
3939

40+
For example::
41+
42+
require __DIR__.'/vendor/autoload.php';
43+
// create a variable, which could be anything!
44+
$someVar = '...';
45+
46+
dump($someVar);
47+
4048
By default, the output format and destination are selected based on your
4149
current PHP SAPI:
4250

4351
* On the command line (CLI SAPI), the output is written on ``STDOUT``. This
4452
can be surprising to some because this bypasses PHP's output buffering
4553
mechanism;
46-
* On other SAPIs, dumps are written as HTML on the regular output.
54+
* On other SAPIs, dumps are written as HTML in the regular output.
4755

4856
.. note::
4957

@@ -101,8 +109,8 @@ original value. You can configure the limits in terms of:
101109
<config max-items="250" max-string-length="-1" />
102110
</container>
103111
104-
Reading a Dump
105-
--------------
112+
Dump Examples and Output
113+
------------------------
106114

107115
For simple variables, reading the output should be straightforward.
108116
Here are some examples showing first a variable defined in PHP,
@@ -115,6 +123,7 @@ then its dump representation::
115123
'a boolean' => true,
116124
'an empty array' => array(),
117125
);
126+
dump($var);
118127

119128
.. image:: /images/components/var_dumper/01-simple.png
120129

@@ -131,6 +140,7 @@ then its dump representation::
131140
$var .= "Non-UTF-8 strings length are counted in octet size.\n";
132141
$var .= "Because of this `\xE9` octet (\\xE9),\n";
133142
$var .= "this string is not UTF-8 valid, thus the `b` prefix.\n";
143+
dump($var);
134144
135145
.. image:: /images/components/var_dumper/02-multi-line-str.png
136146

@@ -144,6 +154,7 @@ then its dump representation::
144154
}
145155
146156
$var = new PropertyExample();
157+
dump($var);
147158
148159
.. image:: /images/components/var_dumper/03-object.png
149160

@@ -161,6 +172,7 @@ then its dump representation::
161172
162173
$var = new DynamicPropertyExample();
163174
$var->undeclaredProperty = 'Runtime added dynamic properties have `"` around their name.';
175+
dump($var);
164176
165177
.. image:: /images/components/var_dumper/04-dynamic-property.png
166178

@@ -172,12 +184,20 @@ then its dump representation::
172184
}
173185
$var = new ReferenceExample();
174186
$var->aCircularReference = $var;
187+
dump($var);
175188
176189
.. image:: /images/components/var_dumper/05-soft-ref.png
177190

178191
.. code-block:: php
179192
180-
$var = new \ErrorException("For some objects, properties have special values\nthat are best represented as constants, like\n`severity` below. Hovering displays the value (`2`).\n", 0, E_WARNING);
193+
$var = new \ErrorException(
194+
"For some objects, properties have special values
195+
that are best represented as constants, like
196+
`severity` below. Hovering displays the value (`2`).",
197+
0,
198+
E_WARNING
199+
);
200+
dump($var);
181201
182202
.. image:: /images/components/var_dumper/06-constants.png
183203

@@ -190,6 +210,7 @@ then its dump representation::
190210
$var[2] = array("Hard references (circular or sibling)");
191211
$var[3] =& $var[2];
192212
$var[3][] = "are dumped using `&number` prefixes.";
213+
dump($var);
193214
194215
.. image:: /images/components/var_dumper/07-hard-ref.png
195216

@@ -199,12 +220,20 @@ then its dump representation::
199220
$var[] = "Some resources and special objects like the current";
200221
$var[] = "one are sometimes best represented using virtual";
201222
$var[] = "properties that describe their internal state.";
223+
dump($var);
202224
203225
.. image:: /images/components/var_dumper/08-virtual-property.png
204226

205227
.. code-block:: php
206228
207-
$var = new AcmeController("When a dump goes over its maximum items limit,\nor when some special objects are encountered,\nchildren can be replaced by an ellipsis and\noptionnally followed by a number that says how\nmany have been removed; `9` in this case.\n");
229+
$var = new AcmeController(
230+
"When a dump goes over its maximum items limit,
231+
or when some special objects are encountered,
232+
children can be replaced by an ellipsis and
233+
optionnally followed by a number that says how
234+
many have been removed; `9` in this case."
235+
);
236+
dump($var);
208237
209238
.. image:: /images/components/var_dumper/09-cut.png
210239

0 commit comments

Comments
 (0)
0