From 6c5ce231e66682e55c09831d45c543ddc77b4b32 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Sat, 14 Jan 2023 20:56:18 +0100 Subject: [PATCH] [DependencyInjection] Autowire union and intersection types --- service_container/autowiring.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/service_container/autowiring.rst b/service_container/autowiring.rst index 084f37587a2..86010b94bc9 100644 --- a/service_container/autowiring.rst +++ b/service_container/autowiring.rst @@ -374,6 +374,30 @@ dealing with the ``TransformerInterface``. discovered that implements an interface, configuring the alias is not mandatory and Symfony will automatically create one. +.. tip:: + + Autowiring is powerful enough to guess which service to inject even when using + union and intersection types. This means you're able to type-hint argument with + complex types like this:: + + use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; + use Symfony\Component\Serializer\Normalizer\NormalizerInterface; + use Symfony\Component\Serializer\SerializerInterface; + + class DataFormatter + { + public function __construct((NormalizerInterface&DenormalizerInterface)|SerializerInterface $transformer) + { + // ... + } + + // ... + } + +.. versionadded:: 5.4 + + The support of union and intersection types was introduced in Symfony 5.4. + Dealing with Multiple Implementations of the Same Type ------------------------------------------------------