5
5
The VarDumper Component
6
6
=======================
7
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 :phpfunction: ` var_dump `.
10
-
8
+ The VarDumper component provides mechanisms for walking through any
9
+ arbitrary PHP variable. Built on top, it provides a better ``dump() ``
10
+ function that you can use instead of :phpfunction: ` var_dump `.
11
11
12
12
.. versionadded :: 2.6
13
13
The VarDumper component was introduced in Symfony 2.6.
@@ -23,23 +23,26 @@ You can install the component in 2 different ways:
23
23
The dump() function
24
24
-------------------
25
25
26
- The VarDumper component creates a global ``dump() `` function that is configured out of the box:
27
- HTML or CLI output is automatically selected based on the current PHP SAPI.
26
+ The VarDumper component creates a global ``dump() `` function that is
27
+ configured out of the box: HTML or CLI output is automatically selected based
28
+ on the current PHP SAPI.
28
29
29
30
The advantages of this function are:
30
31
31
- - per object and resource types specialized view to e.g. filter out Doctrine internals
32
- while dumping a single proxy entity, or get more insight on opened files with
33
- :phpfunction: `stream_get_meta_data() `.
32
+ - per object and resource types specialized view to e.g. filter out
33
+ Doctrine internals while dumping a single proxy entity, or get more
34
+ insight on opened files with :phpfunction: `stream_get_meta_data() `.
34
35
- configurable output formats: HTML or colored command line output.
35
- - ability to dump internal references, either soft ones (objects or resources)
36
- or hard ones (``=& `` on arrays or objects properties). Repeated occurrences of
37
- the same object/array/resource won't appear again and again anymore. Moreover,
38
- you'll be able to inspect the reference structure of your data.
36
+ - ability to dump internal references, either soft ones (objects or
37
+ resources) or hard ones (``=& `` on arrays or objects properties).
38
+ Repeated occurrences of the same object/array/resource won't appear
39
+ again and again anymore. Moreover, you'll be able to inspect the
40
+ reference structure of your data.
39
41
- ability to operate in the context of an output buffering handler.
40
42
41
- ``dump() `` is just a thin wrapper for :method: `VarDumper::dump() <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> `
42
- so can you also use it directly. You can change the behavior of this function by calling
43
+ ``dump() `` is just a thin wrapper for :method: `VarDumper::dump()
44
+ <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> ` so can you also use it directly.
45
+ You can change the behavior of this function by calling
43
46
:method: `VarDumper::setHandler($callable) <Symfony\\ Component\\ VarDumper\\ VarDumper::setHandler> `:
44
47
calls to ``dump() `` will then be forwarded to ``$callable ``, given as first argument.
45
48
@@ -50,9 +53,10 @@ Cloners
50
53
~~~~~~~
51
54
52
55
A cloner is used to create an intermediate representation of any PHP variable.
53
- Its output is a :class: `Data <Symfony\\ Component\\ VarDumper\\ Cloner\\ Data> ` object that wraps this representation.
54
- A cloner also applies limits when creating the representation, so that the corresponding
55
- Data object could represent only a subset of the cloned variable.
56
+ Its output is a :class: `Data <Symfony\\ Component\\ VarDumper\\ Cloner\\ Data> `
57
+ object that wraps this representation. A cloner also applies limits when
58
+ creating the representation, so that the corresponding Data object could
59
+ represent only a subset of the cloned variable.
56
60
57
61
You can create a Data object this way::
58
62
@@ -69,12 +73,14 @@ They will be applied when calling ``->cloneVar()`` afterwards.
69
73
Casters
70
74
~~~~~~~
71
75
72
- Objects and resources nested in a PHP variable are casted to arrays in the intermediate Data representation.
73
- You can tweak the array representation for each object/resource by hooking a Caster into this process.
74
- The component already includes many casters for base PHP classes and other common classes.
76
+ Objects and resources nested in a PHP variable are casted to arrays in the
77
+ intermediate Data representation. You can tweak the array representation for
78
+ each object/resource by hooking a Caster into this process. The component
79
+ already includes many casters for base PHP classes and other common classes.
75
80
76
- If you want to build your own Caster, you can register one before cloning a PHP variable.
77
- Casters are registered using either a Cloner's constructor or its ``addCasters() `` method::
81
+ If you want to build your own Caster, you can register one before cloning
82
+ a PHP variable. Casters are registered using either a Cloner's constructor
83
+ or its ``addCasters() `` method::
78
84
79
85
$myCasters = array(...);
80
86
$cloner = new PhpCloner($myCasters);
@@ -91,21 +97,23 @@ an interface or a resource type to a callable::
91
97
':bar resource' => $myBarResourceCallableCaster,
92
98
);
93
99
94
- As you can notice, resource types are prefixed by a ``: `` to prevent colliding with a class name.
100
+ As you can notice, resource types are prefixed by a ``: `` to prevent
101
+ colliding with a class name.
95
102
96
- Because an object has one main class and potentially many parent classes or interfaces,
97
- many casters can be applied to one object. In this case, casters are called one after the other,
98
- starting from casters bound to the interfaces, the parents classes and then the main class.
99
- Several casters can also be registered for the same resource type/class/interface.
103
+ Because an object has one main class and potentially many parent classes
104
+ or interfaces, many casters can be applied to one object. In this case,
105
+ casters are called one after the other, starting from casters bound to the
106
+ interfaces, the parents classes and then the main class. Several casters
107
+ can also be registered for the same resource type/class/interface.
100
108
They are called in registration order.
101
109
102
- Casters are responsible for returning the properties of the object or resource being cloned in an array.
103
- They are callables that accept four arguments:
110
+ Casters are responsible for returning the properties of the object orresource
111
+ being cloned in an array. They are callables that accept four arguments:
104
112
105
113
- the object or resource being casted,
106
114
- an array modelled for objects after PHP's native ``(array) `` cast operator,
107
- - a :class: `Stub <Sumfony\\ Component\\ VarDumper\\ Cloner\\ Stub> ` object representing
108
- the main properties of the object (class, type, etc.),
115
+ - a :class: `Stub <Sumfony\\ Component\\ VarDumper\\ Cloner\\ Stub> ` object
116
+ representing the main properties of the object (class, type, etc.),
109
117
- true/false when the caster is called nested is a structure or not.
110
118
111
119
Here is a simple caster not doing anything::
@@ -117,18 +125,22 @@ Here is a simple caster not doing anything::
117
125
return $array;
118
126
}
119
127
120
- For objects, the ``$array `` parameter comes pre-populated with PHP's native ``(array) `` casting operator
121
- or with the return value of ``$object->__debugInfo() `` if the magic method exists.
122
- Then, the return value of one Caster is given as argument to the next Caster in the chain.
128
+ For objects, the ``$array `` parameter comes pre-populated with PHP's native
129
+ ``(array) `` casting operator or with the return value of ``$object->__debugInfo() ``
130
+ if the magic method exists. Then, the return value of one Caster is given
131
+ as argument to the next Caster in the chain.
123
132
124
- When casting with the ``(array) `` operator, PHP prefixes protected properties with a ``\0*\0 ``
125
- and private ones with the class owning the property: e.g. ``\0Foobar\0 `` prefixes all private properties
126
- of objects of type Foobar. Casters follow this convention and add two more prefixes: ``\0~\0 `` is used for
127
- virtual properties and ``\0+\0 `` for dynamic ones (runtime added properties not in the class declaration).
133
+ When casting with the ``(array) `` operator, PHP prefixes protected properties
134
+ with a ``\0*\0 `` and private ones with the class owning the property:
135
+ e.g. ``\0Foobar\0 `` prefixes all private properties of objects of type Foobar.
136
+ Casters follow this convention and add two more prefixes: ``\0~\0 `` is used
137
+ for virtual properties and ``\0+\0 `` for dynamic ones (runtime added
138
+ properties not in the class declaration).
128
139
129
140
.. note ::
130
141
131
- Although you can, it is best advised not to alter the state of an object while casting it in a Caster.
142
+ Although you can, it is best advised not to alter the state of an object
143
+ while casting it in a Caster.
132
144
133
145
Dumpers
134
146
~~~~~~~
0 commit comments