@@ -199,9 +199,9 @@ LazyGhostTrait
199
199
200
200
Ghost objects are empty objects, which see their properties populated the first
201
201
time any method is called. Thanks to :class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait `,
202
- the implementation of the lazy mechanism is eased. In the following example, the
203
- `` $hash `` property is defined as lazy. Also, the `` MyLazyObject::computeHash() ``
204
- method should be called only when `` $hash ``'s value need to be known ::
202
+ the implementation of the lazy mechanism is eased. The `` MyLazyObject::populateHash() ``
203
+ method will be called only when the object is actually used and needs to be
204
+ initialized ::
205
205
206
206
namespace App\Hash;
207
207
@@ -219,17 +219,21 @@ method should be called only when ``$hash``'s value need to be known::
219
219
220
220
public function __construct()
221
221
{
222
- self::createLazyGhost(initializer: [
223
- 'hash' => $this->computeHash(...),
224
- ], instance: $this);
222
+ self::createLazyGhost(initializer: $this->populateHash(...), instance: $this);
225
223
}
226
224
227
- private function computeHash (array $data): string
225
+ private function populateHash (array $data): void
228
226
{
229
227
// Compute $this->hash value with the passed data
230
228
}
231
229
}
232
230
231
+ .. deprecated :: 6.4
232
+
233
+ Using an array of closure for property-based initialization in the
234
+ ``createLazyGhost() `` method is deprecated since Symfony 6.4. Pass
235
+ a single closure that initializes the whole object instead.
236
+
233
237
:class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait ` also allows to
234
238
convert non-lazy classes to lazy ones::
235
239
@@ -243,10 +247,10 @@ convert non-lazy classes to lazy ones::
243
247
244
248
public function __construct(array $data)
245
249
{
246
- $this->hash = $this->computeHash ($data);
250
+ $this->populateHash ($data);
247
251
}
248
252
249
- private function computeHash (array $data): string
253
+ private function populateHash (array $data): void
250
254
{
251
255
// ...
252
256
}
@@ -330,10 +334,10 @@ code::
330
334
{
331
335
public function __construct(array $data)
332
336
{
333
- $this->hash = $this->computeHash ($data);
337
+ $this->populateHash ($data);
334
338
}
335
339
336
- private function computeHash (array $data): string
340
+ private function populateHash (array $data): void
337
341
{
338
342
// ...
339
343
}
0 commit comments