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