@@ -177,8 +177,34 @@ populated by using the special ``"\0"`` property name to define their internal v
177
177
"\0" => [$inputArray],
178
178
]);
179
179
180
- Creating Lazy Objects
181
- ---------------------
180
+ Creating Lazy Objects on PHP ≥ 8.4
181
+ ----------------------------------
182
+
183
+ Since version 8.4, PHP provides support for lazy objects via the reflection API.
184
+ This native API works with concrete classes. It doesn't with abstracts nor with
185
+ internal ones.
186
+
187
+ This components provides helpers to generate lazy objects using the decorator
188
+ pattern, which works with abstract or internal classes and with interfaces::
189
+
190
+ $proxyCode = ProxyHelper::generateLazyProxy(new \ReflectionClass(SomeInterface::class));
191
+ // $proxyCode should be dumped into a file in production envs
192
+ eval('class ProxyDecorator'.$proxyCode);
193
+
194
+ $proxy = ProxyDecorator::createLazyProxy(initializer: function (): SomeInterface {
195
+ // [...] Use whatever heavy logic you need here
196
+ // to compute the $dependencies of the proxied class
197
+ $instance = new SomeHeavyClass(...$dependencies);
198
+ // [...] Call setters, etc. if needed
199
+
200
+ return $instance;
201
+ });
202
+
203
+ Use this mechanism only when native lazy objects cannot be leveraged
204
+ (or you'll get a deprecation notice.)
205
+
206
+ Creating Lazy Objects on PHP < 8.3
207
+ ----------------------------------
182
208
183
209
Lazy-objects are objects instantiated empty and populated on-demand. This is
184
210
particularly useful when you have for example properties in your classes that
@@ -193,6 +219,12 @@ you implement such mechanism easily in your classes.
193
219
LazyGhostTrait
194
220
~~~~~~~~~~~~~~
195
221
222
+ .. deprecated :: 7.3
223
+
224
+ ``LazyGhostTrait `` is deprecated since Symfony 7.3; use PHP 8.4's native lazy
225
+ objects instead (note that using the trait with PHP < 8.4 triggers no deprecation
226
+ to help with the transition.)
227
+
196
228
Ghost objects are empty objects, which see their properties populated the first
197
229
time any method is called. Thanks to :class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait `,
198
230
the implementation of the lazy mechanism is eased. The ``MyLazyObject::populateHash() ``
@@ -273,6 +305,12 @@ of :ref:`Virtual Proxies <var-exporter_virtual-proxies>`.
273
305
LazyProxyTrait
274
306
~~~~~~~~~~~~~~
275
307
308
+ .. deprecated :: 7.3
309
+
310
+ ``LazyProxyTrait `` is deprecated since Symfony 7.3; use PHP 8.4's native lazy
311
+ objects instead (note that using the trait with PHP < 8.4 triggers no deprecation
312
+ to help with the transition.)
313
+
276
314
The purpose of virtual proxies in the same one as
277
315
:ref: `ghost objects <var-exporter_ghost-objects >`, but their internal behavior is
278
316
totally different. Where ghost objects requires to extend a base class, virtual
0 commit comments