Skip to content

[VarExporter] Deprecate per-property lazy-initializers #19132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions components/var_exporter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ LazyGhostTrait

Ghost objects are empty objects, which see their properties populated the first
time any method is called. Thanks to :class:`Symfony\\Component\\VarExporter\\LazyGhostTrait`,
the implementation of the lazy mechanism is eased. In the following example, the
``$hash`` property is defined as lazy. Also, the ``MyLazyObject::computeHash()``
method should be called only when ``$hash``'s value need to be known::
the implementation of the lazy mechanism is eased. The ``MyLazyObject::populateHash()``
method will be called only when the object is actually used and needs to be
initialized::

namespace App\Hash;

Expand All @@ -219,17 +219,21 @@ method should be called only when ``$hash``'s value need to be known::

public function __construct()
{
self::createLazyGhost(initializer: [
'hash' => $this->computeHash(...),
], instance: $this);
self::createLazyGhost(initializer: $this->populateHash(...), instance: $this);
}

private function computeHash(array $data): string
private function populateHash(array $data): void
{
// Compute $this->hash value with the passed data
}
}

.. deprecated:: 6.4

Using an array of closures for property-based initialization in the
``createLazyGhost()`` method is deprecated since Symfony 6.4. Pass
a single closure that initializes the whole object instead.

:class:`Symfony\\Component\\VarExporter\\LazyGhostTrait` also allows to
convert non-lazy classes to lazy ones::

Expand All @@ -243,10 +247,10 @@ convert non-lazy classes to lazy ones::

public function __construct(array $data)
{
$this->hash = $this->computeHash($data);
$this->populateHash($data);
}

private function computeHash(array $data): string
private function populateHash(array $data): void
{
// ...
}
Expand Down Expand Up @@ -330,10 +334,10 @@ code::
{
public function __construct(array $data)
{
$this->hash = $this->computeHash($data);
$this->populateHash($data);
}

private function computeHash(array $data): string
private function populateHash(array $data): void
{
// ...
}
Expand Down