symfony / symfony-docs

The Symfony documentation
https://symfony.com/doc
Other
2.16k stars 5.11k forks source link

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

Open Akhristenko opened 10 months ago

Akhristenko commented 10 months ago

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.

javiereguiluz commented 8 months ago

Reading the description of the original code PR (https://github.com/symfony/symfony/pull/40800) I think that the docs tell exactly the same as that original description.

Why do you say that the code can't work as explained in docs? Thanks.