Reputation: 36514
Traditionally, you would use the service container this way:
$container->get('my_service');
However, provided that only one definition of a specific class exists, I'd like to get this service by class name:
$container->xxx('My\Application\Service');
Is this possible with the service container?
Upvotes: 3
Views: 2894
Reputation: 68
It is possible since Symfony 3.3:
For example if you have a UserManager like this:
services:
# ...
# traditional service definition
app.manager.user:
class: AppBundle\EventListener\UserManager
tags: ['kernel.event_subscriber']
then you can get it in the following ways:
// before Symfony 3.3
$this->get('app.manager.user')->save($user);
// Symfony 3.3+
$this->get(UserManager::class)->save($user);
Upvotes: 4
Reputation: 131891
No, it's not possible, because:
[1] Saying, that this one class appears only once is no good argument to implement an edge case into the DIC itself.
Upvotes: 4