Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7165fe6

Browse filesBrowse 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
Copy full SHA for 7165fe6

File tree

Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed

‎service_container/autowiring.rst

Copy file name to clipboardExpand all lines: service_container/autowiring.rst
+24Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.