传统上,您可以这样使用服务容器:
$container->get('my_service');
然而,只要特定类的唯一定义存在,我希望能够通过类名获取此服务:
$container->xxx('My\Application\Service');
使用服务容器可以实现这个吗?
传统上,您可以这样使用服务容器:
$container->get('my_service');
然而,只要特定类的唯一定义存在,我希望能够通过类名获取此服务:
$container->xxx('My\Application\Service');
使用服务容器可以实现这个吗?
不可能,因为:
[1] 说这个类只出现一次并不是将边缘情况实现到DIC本身的好论点。
services:
# ...
# traditional service definition
app.manager.user:
class: AppBundle\EventListener\UserManager
tags: ['kernel.event_subscriber']
// before Symfony 3.3
$this->get('app.manager.user')->save($user);
// Symfony 3.3+
$this->get(UserManager::class)->save($user);
的翻译是:在HTML中,这表示一个带有名称为“link1”的自定义标记,其中“source”参数是该标记的值。