@@ -177,53 +177,53 @@ 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 on PHP ≥ 8.4
181
- ----------------------------------
180
+ Creating Lazy Objects
181
+ ---------------------
182
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.
183
+ Lazy objects are objects instantiated empty and populated on demand. This is
184
+ particularly useful when, for example, a class has properties that require
185
+ heavy computation to determine their values. In such cases, you may want to
186
+ trigger the computation only when the property is actually accessed. This way,
187
+ the expensive processing is avoided entirely if the property is never used.
186
188
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
+ Since version 8.4, PHP provides support for lazy objects via the reflection API.
190
+ This native API works with concrete classes, but not with abstract or internal ones.
191
+ This component provides helpers to generate lazy objects using the decorator
192
+ pattern, which also works with abstract classes, internal classes, and interfaces::
189
193
190
194
$proxyCode = ProxyHelper::generateLazyProxy(new \ReflectionClass(SomeInterface::class));
191
- // $proxyCode should be dumped into a file in production envs
195
+ // $proxyCode should be dumped into a file in production environments
192
196
eval('class ProxyDecorator'.$proxyCode);
193
197
194
198
$proxy = ProxyDecorator::createLazyProxy(initializer: function (): SomeInterface {
195
- // [...] Use whatever heavy logic you need here
199
+ // use whatever heavy logic you need here
196
200
// to compute the $dependencies of the proxied class
197
201
$instance = new SomeHeavyClass(...$dependencies);
198
- // [...] Call setters, etc. if needed
202
+ // call setters, etc. if needed
199
203
200
204
return $instance;
201
205
});
202
206
203
207
Use this mechanism only when native lazy objects cannot be leveraged
204
- (or you'll get a deprecation notice.)
208
+ (otherwise you'll get a deprecation notice).
205
209
206
- Creating Lazy Objects on PHP < 8.3
207
- ----------------------------------
210
+ Legacy Creation of Lazy Objects
211
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208
212
209
- Lazy-objects are objects instantiated empty and populated on-demand. This is
210
- particularly useful when you have for example properties in your classes that
211
- requires some heavy computation to determine their value. In this case, you
212
- may want to trigger the property's value processing only when you actually need
213
- its value. Thanks to this, the heavy computation won't be done if you never use
214
- this property. The VarExporter component is bundled with two traits helping
215
- you implement such mechanism easily in your classes.
213
+ When using a PHP version earlier than 8.4, native lazy objects are not available.
214
+ In these cases, the VarExporter component provides two traits that help you
215
+ implement lazy-loading mechanisms in your classes.
216
216
217
217
.. _var-exporter_ghost-objects :
218
218
219
219
LazyGhostTrait
220
- ~~~~~~~~~~~~~~
220
+ ..............
221
221
222
222
.. deprecated :: 7.3
223
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.)
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 versions earlier than 8.4
226
+ does not trigger a deprecation, to ease the transition.
227
227
228
228
Ghost objects are empty objects, which see their properties populated the first
229
229
time any method is called. Thanks to :class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait `,
@@ -303,13 +303,13 @@ of :ref:`Virtual Proxies <var-exporter_virtual-proxies>`.
303
303
.. _var-exporter_virtual-proxies :
304
304
305
305
LazyProxyTrait
306
- ~~~~~~~~~~~~~~
306
+ ..............
307
307
308
308
.. deprecated :: 7.3
309
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.)
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 versions earlier than 8.4
312
+ does not trigger a deprecation, to ease the transition.
313
313
314
314
The purpose of virtual proxies in the same one as
315
315
:ref: `ghost objects <var-exporter_ghost-objects >`, but their internal behavior is
0 commit comments