Skip to content

Commit 2f62ecc

Browse files
committed
minor #19132 [VarExporter] Deprecate per-property lazy-initializers (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [VarExporter] Deprecate per-property lazy-initializers Fix #19131 Renamed from `computeHash` to `populateHash` and changed return type to `void` to better reflect an initializer at object-level. Commits ------- d17116a [VarExporter] Deprecate per-property lazy-initializers
2 parents e9a88cd + d17116a commit 2f62ecc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

components/var_exporter.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ LazyGhostTrait
199199

200200
Ghost objects are empty objects, which see their properties populated the first
201201
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::
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
234238
convert 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

Comments
 (0)