@@ -199,9 +199,9 @@ LazyGhostTrait
199199
200200Ghost objects are empty objects, which see their properties populated the first
201201time 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 ::
205205
206206 namespace App\Hash;
207207
@@ -219,17 +219,21 @@ method should be called only when ``$hash``'s value need to be known::
219219
220220 public function __construct()
221221 {
222- self::createLazyGhost(initializer: [
223- 'hash' => $this->computeHash(...),
224- ], instance: $this);
222+ self::createLazyGhost(initializer: $this->populateHash(...), instance: $this);
225223 }
226224
227- private function computeHash (array $data): string
225+ private function populateHash (array $data): void
228226 {
229227 // Compute $this->hash value with the passed data
230228 }
231229 }
232230
231+ .. deprecated :: 6.4
232+
233+ Using an array of closures 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+
233237:class: `Symfony\\ Component\\ VarExporter\\ LazyGhostTrait ` also allows to
234238convert non-lazy classes to lazy ones::
235239
@@ -243,10 +247,10 @@ convert non-lazy classes to lazy ones::
243247
244248 public function __construct(array $data)
245249 {
246- $this->hash = $this->computeHash ($data);
250+ $this->populateHash ($data);
247251 }
248252
249- private function computeHash (array $data): string
253+ private function populateHash (array $data): void
250254 {
251255 // ...
252256 }
@@ -330,10 +334,10 @@ code::
330334 {
331335 public function __construct(array $data)
332336 {
333- $this->hash = $this->computeHash ($data);
337+ $this->populateHash ($data);
334338 }
335339
336- private function computeHash (array $data): string
340+ private function populateHash (array $data): void
337341 {
338342 // ...
339343 }
0 commit comments