@@ -90,30 +90,39 @@ file looks like this::
90
90
[]
91
91
);
92
92
93
- Instantiating PHP Classes
94
- -------------------------
93
+ Instantiating & Hydrating PHP Classes
94
+ -------------------------------------
95
95
96
- The other main feature provided by this component is an instantiator which can
97
- create objects and set their properties without calling their constructors or
98
- any other methods::
96
+ Instantiator
97
+ ~~~~~~~~~~~~
98
+
99
+ This component provides an instantiator, which can create objects and set
100
+ their properties without calling their constructors or any other methods::
99
101
100
102
use Symfony\Component\VarExporter\Instantiator;
101
103
102
- // creates an empty instance of Foo
104
+ // Creates an empty instance of Foo
103
105
$fooObject = Instantiator::instantiate(Foo::class);
104
106
105
- // creates a Foo instance and sets one of its properties
107
+ // Creates a Foo instance and sets one of its properties
106
108
$fooObject = Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]);
107
109
108
- // creates a Foo instance and sets a private property defined on its parent Bar class
110
+ The instantiator also allows you to populate the property of a parent class. Assuming
111
+ ``Bar `` is the parent class of ``Foo `` and defines a ``privateBarProperty `` attribute::
112
+
113
+ use Symfony\Component\VarExporter\Instantiator;
114
+
115
+ // Creates a Foo instance and sets a private property defined on its parent Bar class
109
116
$fooObject = Instantiator::instantiate(Foo::class, [], [
110
117
Bar::class => ['privateBarProperty' => $propertyValue],
111
118
]);
112
119
113
120
Instances of ``ArrayObject ``, ``ArrayIterator `` and ``SplObjectHash `` can be
114
121
created by using the special ``"\0" `` property name to define their internal value::
115
122
116
- // Creates an SplObjectHash where $info1 is associated with $object1, etc.
123
+ use Symfony\Component\VarExporter\Instantiator;
124
+
125
+ // Creates an SplObjectStorage where $info1 is associated with $object1, etc.
117
126
$theObject = Instantiator::instantiate(SplObjectStorage::class, [
118
127
"\0" => [$object1, $info1, $object2, $info2...],
119
128
]);
@@ -123,5 +132,52 @@ created by using the special ``"\0"`` property name to define their internal val
123
132
"\0" => [$inputArray],
124
133
]);
125
134
135
+ Hydrator
136
+ ~~~~~~~~
137
+
138
+ The instantiator assumes the object you want to populate doesn't exist yet.
139
+ Somehow, you may want to fill properties of an already existing object. This is
140
+ the goal of the :class: `Symfony\\ Component\\ VarExporter\\ Hydrator `. Here is a
141
+ basic usage of the hydrator populating a property of an object::
142
+
143
+ use Symfony\Component\VarExporter\Hydrator;
144
+
145
+ $object = new Foo();
146
+ Hydrator::hydrate($object, ['propertyName' => $propertyValue]);
147
+
148
+ The hydrator also allows you to populate the property of a parent class. Assuming
149
+ ``Bar `` is the parent class of ``Foo `` and defines a ``privateBarProperty `` attribute::
150
+
151
+ use Symfony\Component\VarExporter\Hydrator;
152
+
153
+ $object = new Foo();
154
+ Hydrator::hydrate($object, [], [
155
+ Bar::class => ['privateBarProperty' => $propertyValue],
156
+ ]);
157
+
158
+ // Alternatively, you can use the special "\0" syntax
159
+ Hydrator::hydrate($object, ["\0Bar\0privateBarProperty" => $propertyValue]);
160
+
161
+ Instances of ``ArrayObject ``, ``ArrayIterator `` and ``SplObjectHash `` can be
162
+ populated by using the special ``"\0" `` property name to define their internal value::
163
+
164
+ use Symfony\Component\VarExporter\Hydrator;
165
+
166
+ // Creates an SplObjectHash where $info1 is associated with $object1, etc.
167
+ $storage = new SplObjectStorage();
168
+ Hydrator::hydrate($storage, [
169
+ "\0" => [$object1, $info1, $object2, $info2...],
170
+ ]);
171
+
172
+ // creates an ArrayObject populated with $inputArray
173
+ $arrayObject = new ArrayObject();
174
+ Hydrator::hydrate($arrayObject, [
175
+ "\0" => [$inputArray],
176
+ ]);
177
+
178
+ .. versionadded :: 6.2
179
+
180
+ The :class: `Symfony\\ Component\\ VarExporter\\ Hydrator ` was introduced in Symfony 6.2.
181
+
126
182
.. _`OPcache` : https://www.php.net/opcache
127
183
.. _`PSR-2` : https://www.php-fig.org/psr/psr-2/
0 commit comments