@@ -90,12 +90,16 @@ file looks like this::
9090 []
9191 );
9292
93- Instantiating PHP Classes
94- -------------------------
93+ .. _instantiating-php-classes :
9594
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::
95+ Instantiating & Hydrating PHP Classes
96+ -------------------------------------
97+
98+ Instantiator
99+ ~~~~~~~~~~~~
100+
101+ This component provides an instantiator, which can create objects and set
102+ their properties without calling their constructors or any other methods::
99103
100104 use Symfony\Component\VarExporter\Instantiator;
101105
@@ -105,6 +109,11 @@ any other methods::
105109 // creates a Foo instance and sets one of its properties
106110 $fooObject = Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]);
107111
112+ The instantiator can also populate the property of a parent class. Assuming ``Bar ``
113+ is the parent class of ``Foo `` and defines a ``privateBarProperty `` attribute::
114+
115+ use Symfony\Component\VarExporter\Instantiator;
116+
108117 // creates a Foo instance and sets a private property defined on its parent Bar class
109118 $fooObject = Instantiator::instantiate(Foo::class, [], [
110119 Bar::class => ['privateBarProperty' => $propertyValue],
@@ -113,7 +122,9 @@ any other methods::
113122Instances of ``ArrayObject ``, ``ArrayIterator `` and ``SplObjectHash `` can be
114123created by using the special ``"\0" `` property name to define their internal value::
115124
116- // Creates an SplObjectHash where $info1 is associated with $object1, etc.
125+ use Symfony\Component\VarExporter\Instantiator;
126+
127+ // creates an SplObjectStorage where $info1 is associated with $object1, etc.
117128 $theObject = Instantiator::instantiate(SplObjectStorage::class, [
118129 "\0" => [$object1, $info1, $object2, $info2...],
119130 ]);
@@ -123,5 +134,52 @@ created by using the special ``"\0"`` property name to define their internal val
123134 "\0" => [$inputArray],
124135 ]);
125136
137+ Hydrator
138+ ~~~~~~~~
139+
140+ Instead of populating objects that don't exist yet (using the instantiator),
141+ sometimes you want to populate properties of an already existing object. This is
142+ the goal of the :class: `Symfony\\ Component\\ VarExporter\\ Hydrator `. Here is a
143+ basic usage of the hydrator populating a property of an object::
144+
145+ use Symfony\Component\VarExporter\Hydrator;
146+
147+ $object = new Foo();
148+ Hydrator::hydrate($object, ['propertyName' => $propertyValue]);
149+
150+ The hydrator can also populate the property of a parent class. Assuming ``Bar ``
151+ is the parent class of ``Foo `` and defines a ``privateBarProperty `` attribute::
152+
153+ use Symfony\Component\VarExporter\Hydrator;
154+
155+ $object = new Foo();
156+ Hydrator::hydrate($object, [], [
157+ Bar::class => ['privateBarProperty' => $propertyValue],
158+ ]);
159+
160+ // alternatively, you can use the special "\0" syntax
161+ Hydrator::hydrate($object, ["\0Bar\0privateBarProperty" => $propertyValue]);
162+
163+ Instances of ``ArrayObject ``, ``ArrayIterator `` and ``SplObjectHash `` can be
164+ populated by using the special ``"\0" `` property name to define their internal value::
165+
166+ use Symfony\Component\VarExporter\Hydrator;
167+
168+ // creates an SplObjectHash where $info1 is associated with $object1, etc.
169+ $storage = new SplObjectStorage();
170+ Hydrator::hydrate($storage, [
171+ "\0" => [$object1, $info1, $object2, $info2...],
172+ ]);
173+
174+ // creates an ArrayObject populated with $inputArray
175+ $arrayObject = new ArrayObject();
176+ Hydrator::hydrate($arrayObject, [
177+ "\0" => [$inputArray],
178+ ]);
179+
180+ .. versionadded :: 6.2
181+
182+ The :class: `Symfony\\ Component\\ VarExporter\\ Hydrator ` was introduced in Symfony 6.2.
183+
126184.. _`OPcache` : https://www.php.net/opcache
127185.. _`PSR-2` : https://www.php-fig.org/psr/psr-2/
0 commit comments