@@ -195,9 +195,9 @@ LazyGhostTrait
195195
196196Ghost objects are empty objects, which see their properties populated the first
197197time 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 ::
201201
202202 namespace App\Hash;
203203
@@ -215,17 +215,21 @@ method should be called only when ``$hash``'s value need to be known::
215215
216216 public function __construct()
217217 {
218- self::createLazyGhost(initializer: [
219- 'hash' => $this->computeHash(...),
220- ], instance: $this);
218+ self::createLazyGhost(initializer: $this->populateHash(...), instance: $this);
221219 }
222220
223- private function computeHash (array $data): string
221+ private function populateHash (array $data): void
224222 {
225223 // Compute $this->hash value with the passed data
226224 }
227225 }
228226
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+
229233:class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait ` also allows to
230234convert non-lazy classes to lazy ones::
231235
@@ -239,10 +243,10 @@ convert non-lazy classes to lazy ones::
239243
240244 public function __construct(array $data)
241245 {
242- $this->hash = $this->computeHash ($data);
246+ $this->populateHash ($data);
243247 }
244248
245- private function computeHash (array $data): string
249+ private function populateHash (array $data): void
246250 {
247251 // ...
248252 }
@@ -322,10 +326,10 @@ code::
322326 {
323327 public function __construct(array $data)
324328 {
325- $this->hash = $this->computeHash ($data);
329+ $this->populateHash ($data);
326330 }
327331
328- private function computeHash (array $data): string
332+ private function populateHash (array $data): void
329333 {
330334 // ...
331335 }
0 commit comments