From 02188fb7469c264c097ed80a2c83b1debcec8c72 Mon Sep 17 00:00:00 2001 From: miqrogroove <1371835+miqrogroove@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:06:48 -0400 Subject: [PATCH] Clarify differences between attributes. --- service_container/autowiring.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/service_container/autowiring.rst b/service_container/autowiring.rst index 483a68c26e9..ef87d3670a6 100644 --- a/service_container/autowiring.rst +++ b/service_container/autowiring.rst @@ -658,7 +658,8 @@ Generate Closures With Autowiring --------------------------------- A **service closure** is an anonymous function that returns a service. This type -of instanciation is handy when you are dealing with lazy-loading. +of instanciation is handy when you are dealing with lazy-loading. It is also +useful for non-shared service dependencies. Automatically creating a closure encapsulating the service instanciation can be done with the @@ -716,7 +717,9 @@ In this case, you can use the :class:`Symfony\Component\DependencyInjection\Attribute\\AutowireCallable` attribute to generate a closure with the same signature as a specific method of a service. When this closure is called, it will pass all its arguments to the underlying service -function:: +function. If the closure needs to be called more than once, the service instance +is reused for repeated calls. Unlike a service closure, this will not +create extra instances of a non-shared service. // src/Service/MessageGenerator.php namespace App\Service; @@ -727,7 +730,7 @@ function:: { public function __construct( #[AutowireCallable(service: 'third_party.remote_message_formatter', method: 'format')] - \Closure $formatCallable + private \Closure $formatCallable ) { }