Skip to content

Commit 7165fe6

Browse files
committed
feature #17748 [DependencyInjection] Autowire union and intersection types (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Autowire union and intersection types Resolves #15927 Commits ------- 6c5ce23 [DependencyInjection] Autowire union and intersection types
2 parents 7d6bc78 + 6c5ce23 commit 7165fe6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

service_container/autowiring.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,30 @@ dealing with the ``TransformerInterface``.
374374
discovered that implements an interface, configuring the alias is not mandatory
375375
and Symfony will automatically create one.
376376

377+
.. tip::
378+
379+
Autowiring is powerful enough to guess which service to inject even when using
380+
union and intersection types. This means you're able to type-hint argument with
381+
complex types like this::
382+
383+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
384+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
385+
use Symfony\Component\Serializer\SerializerInterface;
386+
387+
class DataFormatter
388+
{
389+
public function __construct((NormalizerInterface&DenormalizerInterface)|SerializerInterface $transformer)
390+
{
391+
// ...
392+
}
393+
394+
// ...
395+
}
396+
397+
.. versionadded:: 5.4
398+
399+
The support of union and intersection types was introduced in Symfony 5.4.
400+
377401
Dealing with Multiple Implementations of the Same Type
378402
------------------------------------------------------
379403

0 commit comments

Comments
 (0)