Skip to content

Clarify Lazy Services usage with final & readonly classes #21065

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions service_container/lazy_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ until you interact with the proxy in some way.

.. warning::

Lazy services do not support `final`_ or ``readonly`` classes, but you can use
`Interface Proxifying`_ to work around this limitation.
Lazy services support for `final`_ or ``readonly`` classes was introduced in Symfony 7.3, and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a warning anymore, make let's move this to a versionadded 7.3 directive?

requires PHP 8.4 or newer. You can use `Interface Proxifying`_ to work around this limitation.

.. _lazy-services_configuration:

Expand Down Expand Up @@ -145,9 +145,9 @@ It defines an optional parameter used to define interfaces for proxy and interse
Interface Proxifying
--------------------

Under the hood, proxies generated to lazily load services inherit from the class
used by the service. However, sometimes this is not possible at all (e.g. because
the class is `final`_ and can not be extended) or not convenient.
Under the hood, lazy services leverage `lazy objects`_ since PHP 8.4 and Symfony 7.3. In earlier version,
proxy objects inheriting from the service's class are generated. One of the limitations of proxy objects
is inability to extend `final`_ or ``readonly`` classes.

To workaround this limitation, you can configure a proxy to only implement
specific interfaces.
Expand Down Expand Up @@ -231,4 +231,5 @@ implement multiple interfaces by adding new "proxy" tags.

.. _`ghost object`: https://en.wikipedia.org/wiki/Lazy_loading#Ghost
.. _`final`: https://www.php.net/manual/en/language.oop5.final.php
.. _`lazy objects`: https://www.php.net/manual/en/language.oop5.lazy-objects.php
.. _`proxy`: https://en.wikipedia.org/wiki/Proxy_pattern