Skip to content

Commit 98b6010

Browse files
add examples
1 parent 23712fb commit 98b6010

11 files changed

+121
-12
lines changed

components/var_dumper/advanced.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
Advanced Usage of the VarDumper Component
66
=========================================
77

8+
``dump()`` function is just a thin wrapper and a more convenient way to call
9+
:method:`VarDumper::dump() <Symfony\\Component\\VarDumper\\VarDumper::dump>`.
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``.
13+
814
Cloners
915
~~~~~~~
1016

@@ -46,6 +52,7 @@ method:
4652
the intermediate representation internally.
4753

4854
.. note::
55+
4956
When no limit is applied, a :class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
5057
object is as accurate as the native :phpfunction:`serialize` function
5158
and thus could have a wider purpose than strictly dumping for debugging.
@@ -176,7 +183,7 @@ Here is a simple caster not doing anything::
176183
return $array;
177184
}
178185

179-
For objects, the ``$array`` parameter comes pre-populated with PHP's native
186+
For objects, the ``$array`` parameter comes pre-populated using PHP's native
180187
``(array)`` casting operator or with the return value of ``$object->__debugInfo()``
181188
if the magic method exists. Then, the return value of one Caster is given
182189
as argument to the next Caster in the chain.
@@ -189,5 +196,10 @@ for virtual properties and ``\0+\0`` for dynamic ones (runtime added
189196
properties not in the class declaration).
190197

191198
.. note::
199+
192200
Although you can, it is best advised not to alter the state of an object
193201
while casting it in a Caster.
202+
203+
.. tip::
204+
205+
Before writting your own casters, you should check the existing ones.

components/var_dumper/introduction.rst

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use instead of e.g. :phpfunction:`var_dump`. By using it, you'll gain:
2828

2929
* Per object and resource types specialized view to e.g. filter out
3030
Doctrine internals while dumping a single proxy entity, or get more
31-
insight on opened files with :phpfunction:`stream_get_meta_data()`;
31+
insight on opened files with :phpfunction:`stream_get_meta_data`;
3232
* Configurable output formats: HTML or colored command line output;
3333
* Ability to dump internal references, either soft ones (objects or
3434
resources) or hard ones (``=&`` on arrays or objects properties).
@@ -37,12 +37,6 @@ 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-
``dump()`` is just a thin wrapper and a more convenient way to call
41-
:method:`VarDumper::dump() <Symfony\\Component\\VarDumper\\VarDumper::dump>`.
42-
You can change the behavior of this function by calling
43-
:method:`VarDumper::setHandler($callable) <Symfony\\Component\\VarDumper\\VarDumper::setHandler>`:
44-
calls to ``dump()`` will then be forwarded to ``$callable``.
45-
4640
By default, the output format and destination are selected based on your
4741
current PHP SAPI:
4842

@@ -52,16 +46,17 @@ current PHP SAPI:
5246
* On other SAPIs, dumps are written as HTML on the regular output.
5347

5448
.. note::
49+
5550
If you want to catch the dump output as a string, please read the
56-
`advanced documentation <advanced>` which contains examples of it.
51+
`advanced documentation <advanced>`_ which contains examples of it.
5752
You'll also learn how to change the format or redirect the output to
5853
wherever you want.
5954

6055
DebugBundle and Twig Integration
6156
--------------------------------
6257

6358
The ``DebugBundle`` allows greater integration of the component into the
64-
Symfony full stack framework. It is enabled by default in the dev
59+
Symfony full stack framework. It is enabled by default in the *dev* and *test*
6560
environement of the standard edition since version 2.6.
6661

6762
Since generating (even debug) output in the controller or in the model
@@ -109,8 +104,110 @@ original value. You can configure the limits in terms of:
109104
Reading a Dump
110105
--------------
111106

112-
For simple variables, reading the output should be straightforward::
107+
For simple variables, reading the output should be straightforward.
108+
Here are some examples showing first a variable defined in PHP,
109+
then its dump representation:
110+
111+
.. code-block:: php
112+
113+
$var = array(
114+
'a simple string' => "in an array of 5 elements",
115+
'a float' => 1.0,
116+
'an integer' => 1,
117+
'a boolean' => true,
118+
'an empty array' => array(),
119+
);
120+
121+
.. image:: /images/components/var_dumper/01-simple.png
122+
123+
.. note::
124+
125+
The gray arrow is a toggle button for hidding/showing children of
126+
nested structures.
127+
128+
.. code-block:: php
129+
130+
$var = "This is a multi-line string.\n";
131+
$var .= "Hovering a string shows its length.\n";
132+
$var .= "The length of UTF-8 strings is counted in terms of UTF-8 characters.\n";
133+
$var .= "Non-UTF-8 strings length are counted in octet size.\n";
134+
$var .= "Because of this `\xE9` octet (\\xE9),\n";
135+
$var .= "this string is not UTF-8 valid, thus the `b` prefix.\n";
136+
137+
.. image:: /images/components/var_dumper/02-multi-line-str.png
138+
139+
.. code-block:: php
140+
141+
class PropertyExample
142+
{
143+
public $publicProperty = 'The `+` prefix denotes public properties,';
144+
protected $protectedProperty = '`#` protected ones and `-` private ones.';
145+
private $privateProperty = 'Hovering a property shows a reminder.';
146+
}
147+
148+
$var = new PropertyExample();
149+
150+
.. image:: /images/components/var_dumper/03-object.png
151+
152+
.. note::
153+
154+
`#14` is the internal object handle. It allows comparing two
155+
consecutive dumps of the same object.
156+
157+
.. code-block:: php
158+
159+
class DynamicPropertyExample
160+
{
161+
public $declaredProperty = 'This property is declared in the class definition';
162+
}
163+
164+
$var = new DynamicPropertyExample();
165+
$var->undeclaredProperty = 'Runtime added dynamic properties have `"` around their name.';
166+
167+
.. image:: /images/components/var_dumper/04-dynamic-property.png
168+
169+
.. code-block:: php
170+
171+
class ReferenceExample
172+
{
173+
public $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
174+
}
175+
$var = new ReferenceExample();
176+
$var->aCircularReference = $var;
177+
178+
.. image:: /images/components/var_dumper/05-soft-ref.png
179+
180+
.. code-block:: php
181+
182+
$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);
183+
184+
.. image:: /images/components/var_dumper/06-constants.png
185+
186+
.. code-block:: php
187+
188+
$var = array();
189+
$var[0] = 1;
190+
$var[1] =& $var[0];
191+
$var[1] += 1;
192+
$var[2] = array("Hard references (circular or sibling)");
193+
$var[3] =& $var[2];
194+
$var[3][] = "are dumped using `&number` prefixes.";
195+
196+
.. image:: /images/components/var_dumper/07-hard-ref.png
197+
198+
.. code-block:: php
199+
200+
$var = new \ArrayObject();
201+
$var[] = "Some resources and special objects like the current";
202+
$var[] = "one are sometimes best represented using virtual";
203+
$var[] = "properties that describe their internal state.";
204+
205+
.. image:: /images/components/var_dumper/08-virtual-property.png
206+
207+
.. code-block:: php
208+
209+
$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");
113210
114-
dump(array(true, 1.1, "string"));
211+
.. image:: /images/components/var_dumper/09-cut.png
115212

116213
.. _Packagist: https://packagist.org/packages/symfony/var-dumper
16 KB
Loading
Loading
23.2 KB
Loading
Loading
20.4 KB
Loading
32.4 KB
Loading
19 KB
Loading
Loading
31.9 KB
Loading

0 commit comments

Comments
 (0)