Skip to content

Commit d41f486

Browse files
committed
minor #20955 Update doc about lazy objects (nicolas-grekas)
This PR was merged into the 7.3 branch. Discussion ---------- Update doc about lazy objects Fix #20798 Fix #20761 Commits ------- 33bd9a8 Update doc about lazy objects
2 parents e803408 + 33bd9a8 commit d41f486

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

components/var_exporter.rst

+40-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,34 @@ populated by using the special ``"\0"`` property name to define their internal v
177177
"\0" => [$inputArray],
178178
]);
179179

180-
Creating Lazy Objects
181-
---------------------
180+
Creating Lazy Objects on PHP ≥ 8.4
181+
----------------------------------
182+
183+
Since version 8.4, PHP provides support for lazy objects via the reflection API.
184+
This native API works with concrete classes. It doesn't with abstracts nor with
185+
internal ones.
186+
187+
This components provides helpers to generate lazy objects using the decorator
188+
pattern, which works with abstract or internal classes and with interfaces::
189+
190+
$proxyCode = ProxyHelper::generateLazyProxy(new \ReflectionClass(SomeInterface::class));
191+
// $proxyCode should be dumped into a file in production envs
192+
eval('class ProxyDecorator'.$proxyCode);
193+
194+
$proxy = ProxyDecorator::createLazyProxy(initializer: function (): SomeInterface {
195+
// [...] Use whatever heavy logic you need here
196+
// to compute the $dependencies of the proxied class
197+
$instance = new SomeHeavyClass(...$dependencies);
198+
// [...] Call setters, etc. if needed
199+
200+
return $instance;
201+
});
202+
203+
Use this mechanism only when native lazy objects cannot be leveraged
204+
(or you'll get a deprecation notice.)
205+
206+
Creating Lazy Objects on PHP < 8.3
207+
----------------------------------
182208

183209
Lazy-objects are objects instantiated empty and populated on-demand. This is
184210
particularly useful when you have for example properties in your classes that
@@ -193,6 +219,12 @@ you implement such mechanism easily in your classes.
193219
LazyGhostTrait
194220
~~~~~~~~~~~~~~
195221

222+
.. deprecated:: 7.3
223+
224+
``LazyGhostTrait`` is deprecated since Symfony 7.3; use PHP 8.4's native lazy
225+
objects instead (note that using the trait with PHP < 8.4 triggers no deprecation
226+
to help with the transition.)
227+
196228
Ghost objects are empty objects, which see their properties populated the first
197229
time any method is called. Thanks to :class:`Symfony\\Component\\VarExporter\\LazyGhostTrait`,
198230
the implementation of the lazy mechanism is eased. The ``MyLazyObject::populateHash()``
@@ -273,6 +305,12 @@ of :ref:`Virtual Proxies <var-exporter_virtual-proxies>`.
273305
LazyProxyTrait
274306
~~~~~~~~~~~~~~
275307

308+
.. deprecated:: 7.3
309+
310+
``LazyProxyTrait`` is deprecated since Symfony 7.3; use PHP 8.4's native lazy
311+
objects instead (note that using the trait with PHP < 8.4 triggers no deprecation
312+
to help with the transition.)
313+
276314
The purpose of virtual proxies in the same one as
277315
:ref:`ghost objects <var-exporter_ghost-objects>`, but their internal behavior is
278316
totally different. Where ghost objects requires to extend a base class, virtual

service_container/lazy_services.rst

-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ until you interact with the proxy in some way.
2626
Lazy services do not support `final`_ or ``readonly`` classes, but you can use
2727
`Interface Proxifying`_ to work around this limitation.
2828

29-
In PHP versions prior to 8.0 lazy services do not support parameters with
30-
default values for built-in PHP classes (e.g. ``PDO``).
31-
3229
.. _lazy-services_configuration:
3330

3431
Configuration
@@ -78,11 +75,6 @@ same signature of the class representing the service should be injected. A lazy
7875
itself when being accessed for the first time). The same happens when calling
7976
``Container::get()`` directly.
8077

81-
To check if your lazy service works you can check the interface of the received object::
82-
83-
dump(class_implements($service));
84-
// the output should include "Symfony\Component\VarExporter\LazyObjectInterface"
85-
8678
You can also configure your service's laziness thanks to the
8779
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\Autoconfigure` attribute.
8880
For example, to define your service as lazy use the following::

0 commit comments

Comments
 (0)