Skip to content

Clarify the description of the #[Target] attribute or add examples of usage. #19137

Closed
@Akhristenko

Description

@Akhristenko

Documentation says: Another possibility is to use the #[Target] attribute. By using this attribute on the argument you want to autowire, you can define exactly which service to inject by using its alias.
According to this documentation and having the following code

interface ProviderInterface {}
class ProviderA implement ProviderInterface {}
class ProviderB implement ProviderInterface {}

i define alias for service in services

correct_provider:
  alias: App\ProviderA

and try to inject it via #[Target] attribute

class JobService {
    public function __construct(#[Target('correct_provider')] ProviderInterface $provider) {}
}

but this don't work.
After viewing the source code, I realized that this should not work.
I think this line in the documentation is misleading.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions