Skip to content

[DependencyInjection] Autowire union and intersection types #17748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------------------------------

Expand Down