@@ -658,7 +658,8 @@ Generate Closures With Autowiring
658
658
---------------------------------
659
659
660
660
A **service closure ** is an anonymous function that returns a service. This type
661
- of instanciation is handy when you are dealing with lazy-loading.
661
+ of instanciation is handy when you are dealing with lazy-loading. It is also
662
+ useful for non-shared service dependencies.
662
663
663
664
Automatically creating a closure encapsulating the service instanciation can be
664
665
done with the
@@ -716,7 +717,9 @@ In this case, you can use the
716
717
:class: `Symfony\C omponent\D ependencyInjection\A ttribute\\ AutowireCallable ` attribute
717
718
to generate a closure with the same signature as a specific method of a service. When
718
719
this closure is called, it will pass all its arguments to the underlying service
719
- function::
720
+ function. If the closure needs to be called more than once, the service instance
721
+ is reused for repeated calls. Unlike a service closure, this will not
722
+ create extra instances of a non-shared service.
720
723
721
724
// src/Service/MessageGenerator.php
722
725
namespace App\S ervice;
@@ -727,7 +730,7 @@ function::
727
730
{
728
731
public function __construct(
729
732
#[AutowireCallable(service: 'third_party.remote_message_formatter', method: 'format')]
730
- \Closure $formatCallable
733
+ private \C losure $formatCallable
731
734
) {
732
735
}
733
736
0 commit comments